Packages and init.py

Carson West

Importing Modules

Packages and __init__.py

Python packages are a way of organizing related modules into a hierarchical structure. This helps manage complexity in larger projects. The key to creating a package is the __init__.py file.

# mypackage/__init__.py
__all__ = ["module1", "module2"]] # This line controls what gets imported with `from mypackage import *`

# Or, you can explicitly import modules to make them available:
from .module1 import *
from .module2 import some_function 
mypackage/
├── __init__.py
├── module1.py
└── module2.py
mypackage/
├── __init__.py
├── subpackage1/
│   ├── __init__.py
│   └── module3.py
└── subpackage2/
    └── __init__.py
    └── module4.py