Python, from Zero, for AILesson 27 of 89

Module 3 · Functions, modules, and code you can read again in March

Laying out a project so the imports keep working

In this lesson

  • 8 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 7 of 9 in this chapter

This chapter’s goalSplit a working script into functions and modules with explicit arguments, return values and docstrings, explain from Python’s scoping rules why a variable changed inside a function did or did not change outside it, and run the result from a terminal with named options.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:44Not available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 8Free · no account needed
Lesson steps

A layout that survives contact with reality

For anything past two files:

Text
billing/
├── README.md
├── requirements.txt
├── .gitignore
├── run.py
├── billing/
│   ├── __init__.py
│   ├── money.py
│   └── report.py
└── tests/
    └── test_money.py

The outer billing is the project folder. The inner one is the package: a folder Python can import as a unit. run.py at the top is the entry point:

Python
from billing.money import with_tax

__init__.py

A folder becomes a package when it contains __init__.py. Since Python 3.3 a folder without one can still be imported, as a namespace package, but the rules are subtler and the failure modes are worse. Create the empty file. It costs nothing and removes a category of confusion.

__init__.py runs when the package is first imported, so it is where you re-export the public names:

Python
# billing/__init__.py
from .money import with_tax
from .report import format_report

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘Laying out a project so the imports keep working’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/project-layout (Accessed: 29 November 2026).