Python File Structure
A package is a folder containing Python modules and a special __init__.py file that marks it as a package.
From Files to Organization
As a Python project grows, related code needs an organized structure. Python packages provide that structure: a package is a folder containing Python modules and a special __init__.py file that marks the folder as a package. Packages can also contain subpackages, allowing a project to be arranged across multiple levels.
The organizational pattern is: variables in functions, functions in modules, and modules in packages.
The Containment Hierarchy
A package is not just a collection of unrelated files. It provides a level of containment above individual modules. A module belongs inside a package, and a subpackage is itself a package inside another package. This creates a hierarchy that can group related modules together and then group those groups within a larger project structure.
In this generated arrangement, project_package is the outer package. reports and users are subpackages within it. The files monthly.py, annual.py, and profiles.py represent modules grouped according to the subpackage that contains them. The diagram illustrates the source rule that subpackages are packages within packages and can provide multi-level organization.
Package Parts on Disk
The basic physical arrangement has three roles. The package folder provides the container, __init__.py marks that folder as a package, and Python module files hold the modules inside it. When a package contains subpackages, each folder in that package hierarchy must contain its own __init__.py to be recognized as a package.
A Small Package Arrangement
Arrange two related modules inside a package named analytics.
- 1Create the folder
Use analytics as the package folder.
- 2Add the package marker
Place
__init__.pyinside analytics so the folder is marked as a package. - 3Add modules
Place
sales.pyandcustomers.pyinside analytics as related Python modules.
analytics contains __init__.py, sales.py, and customers.py. The arrangement demonstrates a package containing a special __init__.py file and Python modules.
__init__.py as the Marker
The special filename __init__.py has a structural role: it marks the folder containing it as a package. This is different from an ordinary Python module file. A module is a Python module within the organization; a package is a folder that contains modules and the special marker file.
Module or Package
| Structure | What it contains | Organizational role |
|---|---|---|
| Module | A Python module | A unit inside the file structure |
| Package | Python modules and __init__.py | A folder that organizes modules |
The comparison is structural: a package is a folder that contains modules and the special __init__.py file.
The distinction is about level and containment. A module is one part of the organization, while a package is a containing folder that groups modules. A package may also contain subpackages, so it can represent a larger level of organization than a single module.
Building Subpackages
To organize a larger project, place related modules in a subpackage and place that subpackage inside an outer package. The outer package and every subpackage must contain __init__.py. This preserves the package hierarchy at every folder level.
- 1Choose an outer package folder, such as app.
- 2Place
__init__.pyinside the outer folder. - 3Create subpackage folders for related groups, such as data and reports.
- 4Place
__init__.pyinside every subpackage folder. - 5Place related Python modules inside the appropriate subpackage.
In the generated arrangement, records.py belongs to the data subpackage, while summary.py belongs to the reports subpackage. Both subpackages belong to app. The repeated __init__.py files are what mark app, data, and reports as package folders throughout the hierarchy.
Mistakes in Package Layouts
Common mistake Treating a package as though it were only one module file.
What it looks likeThinking that a package is equivalent to reports.py.
Why it failsA package is a folder containing Python modules and
__init__.py.Do this insteadLook for the containing folder, its __init__.py file, and the modules inside it.
Common mistake Adding __init__.py only to the outer folder.
What it looks likeCreating app/__init__.py but leaving app/data without __init__.py.
Why it failsEvery folder in the package hierarchy must contain
__init__.pyto be recognized as a package.Do this insteadPlace __init__.py in the outer package and in each subpackage folder.
Common mistake Putting related modules at unrelated levels.
What it looks likePlacing one data-related module in the outer package and another in a separate subpackage without an organizing reason.
Why it failsPackages and subpackages are intended to organize modules into a hierarchy.
Do this insteadGroup related modules within an appropriate package or subpackage.
Practice the Structure
Design a package structure for a project that has two groups of related modules: customer-related modules and report-related modules. Include one outer package, one subpackage for each group, and the required __init__.py file in every package folder.
Hints
- Start with one outer package folder.
- Create customer and report subpackage folders inside it.
- Add
__init__.pyto the outer folder and to both subpackage folders. - Place each related module inside its matching subpackage.
Check your answer at three levels: the outer package, each subpackage, and the modules contained by those folders. Every package folder needs __init__.py.
Structure Checklist
- 1A package is a folder containing Python modules and a special
__init__.pyfile. - 2The
__init__.pyfile marks its containing folder as a package. - 3Packages organize modules into a hierarchy, and subpackages are packages within packages.
- 4Every folder in a package hierarchy must contain
__init__.pyto be recognized as a package. - 5A useful organization pattern is variables in functions, functions in modules, and modules in packages.
Key Takeaways
- A module is a Python module within a file structure; a package is a folder that contains modules and
__init__.py. __init__.pymarks its folder as a package.- Packages can contain subpackages, creating multiple levels of organization.
- Every folder in the package hierarchy needs
__init__.py. - The hierarchy follows the pattern of variables in functions, functions in modules, and modules in packages.