Skip to content

Module System: Organization & Packaging

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

  1. Modules & Packages - Organization structure
  2. Import Mechanisms - import vs from...import
  3. name and main - Script vs module behavior
  4. Package Structure - Best practices
  5. 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