Module 14 · Python Modules and Packages — topic 7 of 13
Packages
Grouping modules into packages: package directories, dotted imports, subpackages, absolute and relative imports, and how packages organise large code bases.
Learning objectives
- Create a package with modules
- Import from packages and subpackages
- Use relative imports inside a package
Definition
A package is a directory of modules that is imported as a unit. Modules inside are accessed with dotted names such as shop.pricing, and packages can contain subpackages to any depth.
Introduction
When a project grows beyond a handful of modules, folders keep it organised. In Python, a folder of modules is a package.
Why it matters
Packages give large projects structure and avoid name clashes: shop.models.product and blog.models.product can coexist.
Detailed explanation
A package is a folder containing modules, normally with an __init__.py file (see the next lesson).
Import modules with dotted names: import shop.pricing, from shop import pricing, or from shop.pricing import with_tax.
Packages can contain subpackages: shop/models/product.py is imported as shop.models.product.
Absolute imports use the full dotted path from the top-level package and are the clearest choice.
Inside a package, relative imports use dots: from . import pricing (same package) or from ..models.product import Product (parent package). They only work in modules imported as part of a package.
Package objects have a __path__ attribute listing their folder(s); plain modules don't.
Many standard modules are packages: json, email, http, urllib and concurrent.
The folder that contains the top-level package must be on sys.path. When you run a script or use python -m from the project folder, that happens automatically.
Syntax
- A package is a folder of modules.
- Use dotted names to import.
- Relative imports work inside packages.
Code examples
A simple package
118.0 262.5
Standard packages
True False json.decoder json
More examples
Subpackages and relative imports
270 shop.models.product
Step-by-step explanation
- The services subpackage imports from its sibling models with a relative import (two dots = parent package).
- The top-level script uses absolute dotted imports.
- An alias keeps a long dotted module name short.
Expected output
Running “Subpackages and relative imports” prints exactly:
Practical applications
- Django projects split into apps, each a package.
- Libraries like requests organised into several modules inside one package.
Best practices
- Group related modules into packages.
- Prefer absolute imports for clarity.
- Run package modules with python -m.
Common mistakes
Running a package module directly
Avoid
Prefer
Relative imports need the module to be loaded as part of its package.
Important notes
- Folders without __init__.py can still be imported as 'namespace packages', mainly used to split one package across several locations.
Key takeaways
- A package is a folder of modules.
- Import with dotted names.
- Relative imports use dots inside packages.
Try it yourself
- 1Create a school package with students.py and grades.py and import from both.
- 2Add a subpackage reports with a module that imports from school.grades using a relative import.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
5 questions cover the “Python Modules and Packages” module. Answers and explanations are revealed after you submit.
- ModerateWhat happens the second time a module is imported in the same program?
- ModerateCode under if __name__ == "__main__": runs when…
- ModerateWhat is printed?