Packages and __init__.py
Organize modules into packages with proper directory structure.
Packages and __init__.py is a free Python Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Package?
A package is a directory that contains an __init__.py file and one or more modules. Python treats any such directory as a package you can import from.
mypackage/
__init__.py
utils.py
math_helpers.pyThe __init__.py File
__init__.py runs when the package is imported. It can be empty or expose selected names to callers.
# mypackage/__init__.py
from .utils import helper
from .math_helpers import addImporting from a Package
Use dot-notation to import submodules or names defined in __init__.py.
import mypackage
from mypackage import helper
from mypackage.math_helpers import add
print(add(2, 3))Subpackages
Packages can nest: a subdirectory with its own __init__.py becomes a subpackage.
mypackage/
__init__.py
network/
__init__.py
http.pyRelative Imports
Inside a package, use relative imports with a leading dot to reference sibling modules without hard-coding the package name.
# mypackage/network/http.py
from ..utils import helper # two levels upAbsolute vs Relative Imports
Absolute imports are unambiguous and preferred. Relative imports are useful inside large packages to avoid long names.
# Absolute
from mypackage.utils import helper
# Relative (inside mypackage)
from .utils import helper__all__ in __init__.py
Define __all__ to control what from package import * exports.
# mypackage/__init__.py
__all__ = ['helper', 'add']
from .utils import helper
from .math_helpers import addNamespace Packages (PEP 420)
Python 3.3+ supports namespace packages — directories without __init__.py. Useful for splitting a package across multiple directories.
# No __init__.py needed
# Just have the directory and import normally
import myns.modulePackage __init__ Initialization
Use __init__.py to set package-level constants, configure logging, or lazily load heavy submodules.
# mypackage/__init__.py
import logging
logging.getLogger(__name__).addHandler(logging.NullHandler())
VERSION = '1.0.0'Distributing Packages
Add a pyproject.toml or setup.py to make your package installable with pip.
# pyproject.toml excerpt
[project]
name = "mypackage"
version = "1.0.0"
# Install locally
# pip install -e .Inspecting Package Attributes
Use __file__, __path__, and __name__ to introspect package metadata at runtime.
import mypackage
print(mypackage.__file__) # path to __init__.py
print(mypackage.__path__) # list of directory paths
print(mypackage.__name__) # 'mypackage'Quick Check
Which file makes a directory a Python package?
Recap
Packages organize modules into directories using __init__.py. Use absolute imports for clarity, relative imports within a package, and __all__ to control public APIs.
Frequently asked questions
Is the “Packages and __init__.py” lesson free?
Yes — the full text of “Packages and __init__.py” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “Packages and __init__.py”?
Organize modules into packages with proper directory structure. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Packages and __init__.py” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Creating and Importing Modules
- The Python Standard Library
- Packages and __init__.py
- Installing Packages with pip