▶ Sign up free for the video, slides & prompts
The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.
Checking…
Virtual environments
Keep each project's libraries separate, with venv.
🎬 Video version coming soon — read it now
Project A needs pandas 1.5. Project B needs pandas 2.2. If both install into the same Python, one of them breaks. A virtual environment (venv) gives each project its own private set of packages.
Creating one
In your project folder, in a terminal:
This creates a .venv folder containing a copy of Python and an empty package collection.
Activating it
Your terminal prompt now starts with (.venv). Anything you pip install goes into this project only.
Type deactivate to leave it.
The everyday routine
- Make a project folder
python -m venv .venv- Activate it
- Install what you need
pip freeze > requirements.txt- Add
.venvto.gitignore. Never upload the venv to GitHub, only requirements.txt
When someone else (or you, on a new laptop) gets the project:
Do I need this in Colab?
No. Each Colab notebook already runs in its own fresh machine. Virtual environments matter when you run Python on your own laptop, which is the next lesson.
Other tools you'll hear about
- uv: a very fast modern replacement for pip and venv, increasingly popular
- conda: common in data science, also manages non-Python libraries
- poetry: manages dependencies and packaging together
They all solve the same problem. Learn venv first; the ideas carry over.
In one line
A virtual environment gives each project its own packages: create it with python -m venv .venv, activate it, and share requirements.txt.
Share with a friend