Sub topic 1
Why isolate your Python environment
Every data analysis project depends on specific Python packages at specific versions. This chapter explains why those packages should live in an isolated environment for each project, and what goes wrong when they don't. Later chapters show how to create and use one.
Prerequisites
This chapter assumes you have:
- Basic Python syntax, including imports.
- Comfort using a terminal or command prompt.
- Python 3 installed on your machine.
- Familiarity with CSV files and tabular data.
You don't need Jupyter installed yet.
The problem: one Python, many projects
When you install Python, you get one interpreter and one place where third-party packages are stored, called site-packages. If you run pip install pandas with no environment active, pip writes into that shared location. Every script and notebook on your machine then sees the same copy of pandas.
That works until two projects disagree. Suppose project A was written against pandas 1.5 and project B needs a feature from pandas 2.2. Only one version can be installed in the shared location. Upgrading for B can silently change the behavior of A, and the failure may show up weeks later as a changed result or an error in code you haven't touched.
What an isolated environment gives you
A virtual environment is a directory that holds its own copy of (or link to) the Python interpreter and its own site-packages. When it is active, python and pip refer to that directory instead of the system install.
This gives you four benefits:
- No version conflicts. Each project pins the versions it needs.
- A safe system Python. Operating systems often use their own Python for internal tools. Changing its packages can break them.
- Easy cleanup. Deleting the environment directory removes everything the project installed.
- A clear dependency list. A fresh environment contains only what you installed, so the list of requirements is accurate and short.
How Python chooses which packages to load
When you run import pandas, Python searches the directories in sys.path in order and uses the first match. The active interpreter determines which site-packages directory is on that path. You can check which interpreter and location you are using:
import sys
print(sys.executable)
print(sys.prefix)
print([p for p in sys.path if "site-packages" in p])Run this in a terminal Python session and later in a notebook. If the paths differ, the notebook is using a different environment from the one you think you installed into. This mismatch is the most common Jupyter setup error, and the kernels chapter returns to it.
How the pieces fit together
Jupyter adds a layer: the JupyterLab server runs in one Python, but each notebook runs code in a separate process called a kernel. The kernel's interpreter decides which packages the notebook can import.
Common errors that isolation prevents
| Symptom | Typical cause |
|---|---|
| `ModuleNotFoundError` in a notebook for a package you just installed | `pip` installed into a different Python than the kernel uses |
| Code works on your laptop but fails on a colleague's | Different package versions, with no record of yours |
| A working notebook breaks after an unrelated install | A shared package was upgraded or downgraded |
| `pip` refuses to install, citing an "externally managed environment" | The operating system protects its own Python |
What comes next
The following chapter creates a virtual environment with python -m venv, activates it, and installs JupyterLab, pandas, and matplotlib into it. A virtual environment is the simplest tool for this. Conda and similar tools solve the same problem with extra features, but this tutorial uses venv because it ships with Python.
Exercise
You run pip install pandas in one terminal, then open a notebook and get ModuleNotFoundError: No module named 'pandas'. Name two likely causes and one command or snippet that helps you tell which it is.
Show answer
Likely causes: (1) the pip you ran belongs to a different Python than the notebook's kernel, or (2) the notebook uses a kernel from a different environment, so the package is not on its sys.path. To check, run import sys; print(sys.executable) in the notebook and compare it with the output of python -c "import sys; print(sys.executable)" in the terminal where you installed. If they differ, install with the kernel's interpreter (python -m pip install pandas) or switch kernels.