Modules and Imports
- Modules. Every .py file is a module with its own namespace: import runs it once and caches the result.
- Import forms. import math keeps the namespace; from math import sqrt pulls one name in directly: both have their place.
- __name__. The __name__ variable equals "__main__" only when the file is run directly: the guard that keeps imports side-effect free.
Packages and Project Structure
- Packages. A package is a directory of modules with an __init__.py, letting imports nest: from shop.orders import checkout.
- Project layout. A healthy project separates src code, tests and configuration: tooling and imports both rely on that shape.
- Absolute imports. Prefer absolute imports naming the full package path: relative dots break the moment files move.
Virtual Environments and Dependencies
- Virtual environments. A virtual environment gives each project its own installed packages, so two projects never fight over versions.
- pip and requirements. pip installs packages from PyPI, and a pinned requirements file makes the same install reproducible anywhere.
- Dependency hygiene. Every dependency is code you now ship: pin versions, audit what you add, and prune what you stopped using.
Before and after this level
- Before: Level 4: Functions & Scope: Functions, Parameters and Returns, Scope and Name Resolution, Functional Patterns and Lambdas.
- Next: Level 6: Object-Oriented Python: Classes and Objects, Inheritance, Composition and Protocols, Dataclasses and Domain Models.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does Python Level 5: Modules & Packages cover?
Modules and Imports, Packages and Project Structure, Virtual Environments and Dependencies: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 4: Functions & Scope, which covers Functions, Parameters and Returns, Scope and Name Resolution, Functional Patterns and Lambdas.