1. Modules and packages
- Module — A single Python file (
.py) containing functions, classes, and variables. - Package — A directory containing multiple modules and a special
__init__.pyfile that marks it as a package.
Best practices for imports
Always use absolute imports. Relative imports (likefrom ..utils import db) are fragile — they break the moment you run a file as a standalone script rather than as part of a package. Use the full path from the project root instead:
from module import * silently pollutes your namespace and can overwrite existing names without any warning. Always import exactly what you need:
2. Virtual environments
A virtual environment is a self-contained directory with its own Python executable and its own installed packages. Every project should have one.Why isolate environments?
By default,pip install installs packages globally. If Project A needs django==3.2 and Project B needs django==4.2, they can’t both be satisfied in a single global installation. Virtual environments solve this by giving each project its own isolated set of packages.
Creating and activating a virtual environment (standard)
pip install adds packages only to .venv — your global Python is untouched.
Modern alternative: uv
uv is an ultra-fast Python package manager and virtual environment tool written in Rust. It serves as a drop-in replacement for pip and venv but runs 10–100× faster:
What’s next?
Now that you’ve completed practical Python, let’s learn how to extend your programs using standard library modules and third-party packages.Extending Python
Standard library and external package management