Skip to main content

Python Tutorial 6.3 - Package Structure in Python

Welcome to the “Package Structure” tutorial!


Overview

A package is a directory with an __init__.py that groups modules.

Example Layout

myproject/
├── mypackage/
│   ├── __init__.py
│   ├── mod1.py
│   └── mod2.py
└── main.py
  • __init__.py can be empty or expose submodules:

    # mypackage/__init__.py
    from .mod1 import foo
    from .mod2 import bar
    
  • Importing:

    from mypackage import foo, bar
    

Key Takeaways

  • Use packages to avoid naming conflicts.
  • Keep related modules together in a folder.

Happy structuring!

Comments