Tutorials · Chapter D (4/4) · ~9 min
Python virtual environments
Try it → see it → read → next
Give each Python project its own package shelf so experiments stay reproducible.
Try yourself
Playground
Env conflict
Two projects need different package versions. A global install can’t please both.
Recap
What you just did
EnvConflictSim broke two projects with clashing packages, then fixed it with a venv per project.
Teach
How it works
From a project directory:
python -m venv .venv
source .venv/bin/activate
python -m pip install pandas
python -m pip freeze > requirements.txt
On Windows, activation is usually .venv\Scripts\activate.
- Create
.venvwith a specific Python - Activate so
pythonandpippoint inside it - Install only what this project needs
- Record dependencies so someone else can rebuild them
Mental model: each project gets a labeled toolbox instead of borrowing loose tools from one giant pile.
Use it
When you'd use this
- Starting any data science or AI repository
- Testing a package upgrade without breaking another project
- Reproducing a teammate’s dependency list
Watch out
Watch out
Do not commit the .venv directory; it is large and machine-specific. Commit a dependency file instead. If an import still fails after installation, confirm your editor and terminal are using the environment’s Python.
Try next
Try this next
Deactivate the environment, check the Python path, reactivate, and check again. Explain why the paths differ.