Sub topic 13
Isolating a project with a virtual environment
This chapter assumes you can open a terminal, run python3 --version successfully, and run a script file. You do not need prior experience with packages.
Why isolate a project
Python installs packages into a single shared location by default. If project A needs version 1 of a library and project B needs version 2, they conflict. Installing packages into the system Python can also break tools that your operating system relies on.
A virtual environment is a folder that holds its own copy of (or link to) the Python interpreter and its own set of installed packages. Each project gets one, so projects cannot interfere with each other. The standard tool for creating one is the venv module, which ships with Python.
How it works
A virtual environment does not copy the whole standard library. It creates a small folder with these parts:
- A
pyvenv.cfgfile that records which base Python created it. - A
binfolder (Scriptson Windows) holding apythoncommand that points to the base interpreter. - A
lib/.../site-packagesfolder where installed packages go.
When you run the environment's python, it sees that pyvenv.cfg and uses the environment's site-packages instead of the shared one.
Create and activate an environment
Run these commands from your project folder. The first creates the environment in a folder named .venv. The second activates it.
python3 -m venv .venv
source .venv/bin/activate
# On Windows (Command Prompt): .venv\Scripts\activate.bat
# On Windows (PowerShell): .venv\Scripts\Activate.ps1After activation, your prompt usually starts with (.venv). Activation changes PATH for that terminal window only, so the environment's python is found first. Closing the terminal ends the activation. The environment itself stays on disk.
To leave the environment, run deactivate.
Install a package and confirm where it went
With the environment active, use python -m pip so that pip is guaranteed to belong to the same interpreter you are running.
python -m pip install requests
python -m pip listThe list shows only the packages in this environment, which for a new environment is pip plus what you just installed. To confirm which interpreter is active, run this script:
import sys
print(sys.prefix)
print(sys.base_prefix)Inside an active environment, sys.prefix points to your .venv folder and sys.base_prefix points to the base installation. Outside one, both are the same.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `No module named requests` after installing | You installed with a different Python than the one running your script | Activate the environment, then run `python -m pip install requests` and `python script.py` |
| PowerShell says running scripts is disabled | The execution policy blocks `Activate.ps1` | Use Command Prompt, or allow local scripts for your user with `Set-ExecutionPolicy -Scope CurrentUser RemoteSigned` |
| `venv` creation fails on Debian or Ubuntu | The `python3-venv` package is not installed | Install it with `sudo apt install python3-venv` |
| Environment stops working after moving the folder | Environments store absolute paths | Delete it and create a new one in the new location |
Exercise
Exercise
You activated .venv, installed requests, then opened a second terminal window and ran python3 script.py, which imports requests. It fails with "No module named requests". Why, and what do you do?
Show answer
Activation affects only the terminal window where you ran it. The second window uses the system Python, which does not have requests. Activate the environment in the second window (source .venv/bin/activate), or run the script with the environment's interpreter directly (.venv/bin/python script.py).
Exercise
Inside an active environment, how can you tell from Python code that you are not using the base installation?
Show answer
Compare sys.prefix with sys.base_prefix. In an active virtual environment they differ, because sys.prefix points to the .venv folder.