Sub topic 2
Create a virtual environment for the project
This chapter assumes you have Python 3 installed, can use a terminal, and know basic Python syntax. You will create an isolated environment for one project and confirm that it works.
What a virtual environment is
A virtual environment is a folder that holds its own copy of the Python interpreter settings and its own site-packages directory. When you activate it, the python and pip commands on your shell path point to that folder instead of your system installation. Packages you install go there and nowhere else.
This keeps projects apart. One project can use pandas 1.5 while another uses pandas 2.2, and neither affects the system Python.
The standard library includes the venv module, so you need no extra tools.
Create the environment
Open a terminal in your project folder and run:
python -m venv .venvThis creates a .venv folder in the project. Using .venv as the name is a common convention. Many editors detect it automatically, and it is easy to exclude from Git.
If your system uses python3 instead of python, use that command for this step. After activation, python will work.
Activate it
Activation changes your current shell session so it uses the environment.
source .venv/bin/activate # Windows: .venv\Scripts\activateOn Windows PowerShell, the script is .venv\Scripts\Activate.ps1. If PowerShell refuses to run it, you may need to allow local scripts for your user with Set-ExecutionPolicy -Scope CurrentUser RemoteSigned.
Your prompt usually gains a (.venv) prefix. Activation lasts only for that terminal window. Open a new terminal and you must activate again.
Verify it
Do not trust the prompt alone. Ask Python where it lives:
python -c "import sys; print(sys.prefix)"The printed path should end in .venv. On macOS and Linux you can also run which python. On Windows, use where python. The first result should be inside .venv.
Then update pip inside the environment:
python -m pip install --upgrade pipRunning pip as python -m pip guarantees that you use the pip belonging to the active Python. A bare pip command can resolve to a different installation if your path is misconfigured.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `No module named venv` on Debian or Ubuntu | The venv package is not installed | Install it with `sudo apt install python3-venv` |
| `python: command not found` | Only `python3` exists on your system | Use `python3 -m venv .venv` |
| Packages install but imports fail later | The environment was not active when you installed | Activate it, then reinstall |
| Environment breaks after moving the folder | Environments store absolute paths | Delete `.venv` and recreate it |
Never copy or move a .venv folder to share it. Recreate it from a list of dependencies instead. A later chapter covers how to record that list.
Keep it out of Git
The environment is large and specific to your machine. If you use Git, add it to .gitignore:
echo ".venv/" >> .gitignoreLeave the environment
When you finish, run deactivate. Your shell returns to the system Python. The .venv folder stays on disk, so you can activate it again later.
Exercise
You activated .venv, ran pip install pandas, and then import pandas fails inside Jupyter. Give one way to check which Python your pip used, and one reason it might not be the environment's Python.
Show answer
Run python -m pip --version, which prints the path of the pip tied to the active Python, or compare which pip (where pip on Windows) with which python. A bare pip may belong to a different installation if the environment was not actually active in that terminal, or if the shell path resolves pip to another Python. Using python -m pip avoids the mismatch.