Module System¶
Overview¶
Module system organizes Python code:
- Modules: Single.py files
- Packages: Directories with init.py
- Imports: Different import styles
- *all*: Public API declaration
- Implications for ML: Framework structure, plugin systems
Key Topics¶
- Modules & Packages - Organization structure
- Import Mechanisms - import vs from...import
- *name* and main**** - Script vs module behavior
- Package Structure - Best practices
- sys.modules - Runtime module management
Organization Best Practices¶
myproject
- __init__.py # Package marker
- setup.py # Installation config
- README.md
- mymodule
- __init__.py # Package init
- core.py # Core functionality
- utils.py # Utilities
- __main__.py # CLI entry point
- tests
- __init__.py
- test_core.py
Import Styles¶
# Absolute import
from myproject.mymodule.core import Model
# Relative import (from within package)
from.core import Model
from..utils import helper
# Conditional import
try:
import torch
except ImportError:
torch = None
all Declaration¶
# In module __init__.py
__all__ = ['Model', 'Trainer', 'predict']
# Only these are exported with "from module import *"
# Everything else is considered private
Common Patterns¶
| Pattern | Use Case |
|---|---|
| Plugin system | Dynamic module loading |
| Lazy imports | Reduce startup time |
| Conditional imports | Optional dependencies |
| *main* check | Script vs module behavior |
-
Related Topics¶
- [02 Execution Model & Compilation](/05-py3/09-bytecode-and-execution/(02-execution-model-compilation/) - Module compilation
- [05 Jax & Functional Ml](/05-py3/03-functional-programming/(05-jax-functional-ml/) - Module structure in ML frameworks