Learn with diagrams, code, systems and practical examples.
Virtual Environments and Dependencies
Keep project dependencies isolated and reproducible.
Different projects may need different package versions, and global installs quickly become difficult to reason about.
Start with the idea
A virtual environment provides an isolated Python environment for one project. Activate it before installing project-specific dependencies and record those dependencies in the project configuration.
Small working example
Run this example first. Do not change several things at once; confirm the basic behavior, then experiment.
python -m venv .venv
# activate the environment for your shell
python -m pip install requests A virtual environment provides an isolated Python environment for one project. Activate it before installing project-specific dependencies and record those dependencies in the project configuration.
- Virtual environments isolate dependencies.
- Use python -m pip for interpreter clarity.
- Reproducible projects record dependency requirements.
The example is intentionally small. Focus on the chapter’s main idea before adding extra syntax or framework code.
Common beginner mistake
Installing every package globally and later not knowing which project depends on which version.
Use a project-local environment and maintain explicit dependency metadata.
Quick recap
- Virtual environments isolate dependencies.
- Use python -m pip for interpreter clarity.
- Reproducible projects record dependency requirements.
Try it yourself
These are deliberately small. If you can complete them without copying the example, you are ready to continue.
- Create a .venv for a sample project.
- Install one package and verify it inside the environment.