Sub topic 5
Isolating the environment
This chapter assumes you can run Python and Jupyter, use a terminal, and install packages with pip. It covers one step: giving each project its own set of packages, so that a change elsewhere on your machine cannot alter your results.
Why a shared environment breaks results
Python installs packages into one place by default. If two projects share that place, upgrading pandas for project B can silently change the output of project A. A notebook that ran last month may then fail, or worse, run and return different numbers.
A virtual environment is a folder that holds its own Python interpreter link and its own installed packages. Each project gets one, so installs in one project never touch another.
Creating a project-local environment
Create the environment inside the project folder. The name .venv is a common convention that most editors and tools recognize.
python -m venv .venv
source .venv/bin/activate
pip install pandas matplotlib
pip freeze > requirements.txt
# Later, on another machine:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtOn Windows, activate with .venv\Scripts\activate instead of the source line.
The first four commands build the environment, switch into it, install packages, and save the exact versions. The last three recreate it elsewhere. Pinning those versions is covered in the next chapter.
Connecting the environment to Jupyter
A common error is installing packages in the environment but launching Jupyter from somewhere else. The notebook then runs on a different Python than the one you set up. Register the environment as a kernel, which is the process that runs your notebook cells.
pip install ipykernel
python -m ipykernel install --user --name my-analysis --display-name "Python (my-analysis)"Then pick "Python (my-analysis)" from the kernel menu. Check it from inside the notebook:
import sys
print(sys.executable)The printed path should contain your project's .venv folder. If it points to a system location, the notebook is using the wrong interpreter.
Common errors
- Installing with the wrong pip. A bare
pipmay belong to another Python. Usepython -m pip install ...so the install goes to the interpreter you are running. - Forgetting to activate. If your prompt does not show
(.venv), installs go to the global Python. - Using
!pip installin a cell. This installs into whichever environment the kernel uses, but leaves no record. Install from the terminal and updaterequirements.txt, or use%pip installand record the result. - Committing
.venvto Git. The folder is large and tied to your machine. Add.venv/to.gitignoreand sharerequirements.txtinstead.
venv or conda
| Tool | Strength | Trade-off |
|---|---|---|
| `venv` with pip | Ships with Python, simple | Cannot install a different Python version or non-Python libraries |
| conda | Manages Python version and compiled libraries | Larger, with its own file format (`environment.yml`) |
Choose conda when your analysis needs a specific Python version or system libraries. Otherwise venv is enough. Either way, the rule is the same: one environment per project, recorded in a file.
Exercise
You installed pandas in .venv, but import pandas in your notebook reports an older version than the one you installed. Which two checks find the cause, and what is the likely fix?
Show answer
Run import sys; print(sys.executable) in the notebook and check that it points to the project's .venv. Also compare the output of python -m pip show pandas in the activated terminal. If the notebook uses a different interpreter, register the .venv as a kernel with ipykernel and select it in the notebook.