Sub topic 1
Why isolate your Python environment
A Jupyter setup for data analysis depends on many installed packages: JupyterLab, pandas, matplotlib, and the libraries they need. Where those packages are installed decides whether your notebook works today, works on a colleague's machine, and still works in six months. This chapter explains what a Python environment is, how sharing one environment between projects breaks things, and how isolation prevents that. Later chapters create and use the environment.
Prerequisites
This chapter assumes you have:
- Basic Python syntax, enough to read short scripts.
- Comfort with a terminal or command prompt: changing directories and running commands.
- Python 3 installed, with
python --versionworking (on some systems the command ispython3). - Familiarity with CSV files and tabular data. You will need this in later chapters.
You do not need to know how pip resolves dependencies or how venv works internally. This chapter covers both at the level you need.
What a Python environment is
A Python environment is the set of things that decide what import something does when you run Python. It has four parts:
- An interpreter. This is the
pythonexecutable that runs your code. - A standard library. This is the modules that ship with that interpreter version.
- A site-packages directory. This is where
pipplaces third-party packages such as pandas. - Command-line scripts. Installing a package can add commands such as
jupyterorpipto abinfolder (Scriptson Windows).
Which environment you use is mostly decided by which python your shell finds first on its PATH. Many machines have several. Examples are the one the operating system ships, one from python.org, one from a package manager, and one inside a project folder. Each has its own site-packages directory, and installing into one does not change the others.
What goes wrong without isolation
If you install everything with a bare pip install into a single shared interpreter, you are using one global environment for every project. This causes four kinds of problems.
Version conflicts between projects
Project A was written against pandas 1.5 and project B needs features from pandas 2.2. A site-packages directory holds one version of each package, so installing for B upgrades pandas and can break A. The failure usually appears later, as a changed default, a removed function, or a new warning, in a notebook you did not touch.
Damage to the system Python
Operating systems and tools often rely on their own Python. Upgrading or removing packages in that interpreter can break system utilities. Recent Linux distributions and Homebrew builds follow PEP 668 and refuse the install:
error: externally-managed-environmentThis error means the interpreter is protecting itself. Do not bypass it with --break-system-packages. Create an environment instead.
Packages that cannot be traced
After a year of installing things globally, you cannot tell which packages your analysis needs and which are leftovers from other work. Without that list you cannot rebuild the setup on another machine. A project-only environment contains only what that project installed, so the dependency list is short and accurate.
Notebook and terminal disagreeing
Jupyter adds a particular problem. The notebook runs code in a separate process called a kernel, which uses one specific interpreter. If you pip install in a terminal that uses interpreter X while the notebook's kernel uses interpreter Y, the package lands in the wrong place. You then see ModuleNotFoundError in a notebook for a package you just installed. Kernels get their own chapter. Here the point is that without a clear one-project, one-environment rule, you can no longer tell which interpreter is involved.
How isolation works
The standard library module venv creates a virtual environment: a folder holding a small interpreter link, an empty site-packages directory, and a configuration file. It does not copy the whole of Python. It points back to the base interpreter for the standard library and gives your project its own place for third-party packages.
Activating the environment prepends its bin (or Scripts) folder to your shell's PATH. After that, python, pip, and jupyter resolve to the copies inside the environment. Activation changes only the current shell session. Closing the terminal ends it.
Because the two site-packages directories are separate, project B can use pandas 2.2 without affecting project A.
Checking which environment is active
This script reports which interpreter runs it and whether that interpreter is a virtual environment. Save it as whereami.py:
import sys
print("executable :", sys.executable)
print("prefix :", sys.prefix)
print("base prefix:", sys.base_prefix)
print("isolated :", sys.prefix != sys.base_prefix)Outside a virtual environment, prefix and base prefix are the same, and isolated prints False. Inside one, prefix points to your .venv folder and isolated prints True. You can paste the same lines into a notebook cell to see which interpreter its kernel uses. That is the quickest check when a package import fails.
You can also ask pip where it installs things:
python -m pip --versionThe output ends with the path of the Python it belongs to, for example (python 3.12) after a location inside .venv/lib/.../site-packages. If that location is not your project folder, you are about to install globally.
Use `python -m pip`, not bare `pip`
On a machine with several Pythons, the pip on your PATH may belong to a different interpreter than the python next to it. Running python -m pip install ... guarantees that the pip used is the one attached to that python. This tutorial uses that form throughout.
Common errors and what causes them
| Symptom | Likely cause | Fix |
|---|---|---|
| `ModuleNotFoundError` in a notebook after a successful `pip install` | The package went into a different interpreter than the notebook's kernel | Run `import sys; print(sys.executable)` in the notebook, then install with `python -m pip` using that same interpreter |
| `error: externally-managed-environment` | You are installing into an OS-managed Python (PEP 668) | Create and activate a virtual environment, then retry |
| `jupyter: command not found` right after installing | The environment is not activated, or JupyterLab went into another one | Activate `.venv` and check with `python -m pip show jupyterlab` |
| A notebook that worked last month now raises `AttributeError` or `TypeError` | A shared package was upgraded by another project | Give each project its own environment and record versions |
| Activation script does nothing on Windows PowerShell | Script execution policy blocks it | Use `.venv\Scripts\Activate.ps1` after allowing scripts for your user, or use Command Prompt with `activate.bat` |
| The environment breaks after you move or rename its folder | Virtual environments store absolute paths | Delete the folder and recreate it from your dependency list |
The last row shows an important property: a virtual environment is disposable. Do not treat it as a project asset. Treat the list of what to install as the asset, and rebuild the environment whenever it is damaged.
Choosing an isolation tool
Several tools provide isolation. They differ in what they manage.
| Tool | What it isolates | Typical use |
|---|---|---|
| `venv` | Python packages, using the interpreter you already have | Default choice, included in the standard library |
| `virtualenv` | Same as `venv`, with extra features and faster creation | Older projects or special needs |
| conda | Packages and the Python version, plus non-Python libraries | Projects needing compiled scientific libraries |
| `uv`, Poetry, Pipenv | Packages, plus dependency locking and project workflow | Teams that want locked dependency files |
This tutorial uses venv and pip because they need nothing beyond a standard Python install and the same ideas carry over to the other tools. If your team already uses conda or uv, keep using it. The rule that matters is one environment per project.
Conventions that keep environments manageable
- One environment per project. Do not share one across unrelated analyses.
- Keep it inside the project folder. The name
.venvis widely recognised by editors and tools. - Never commit it. Add
.venv/to.gitignore. It is large, tied to your machine, and rebuildable. - Do not install packages as administrator.
sudo pip installwrites into system locations and defeats the purpose. - Install tools into the environment. JupyterLab itself should live in the project environment, not in a global location, so the notebook server, the kernel, and your libraries agree about versions.
What comes next
The next chapter creates a virtual environment with python -m venv, activates it on each major operating system, and confirms it is active using the checks from this chapter.
Exercise
You run pip install pandas in your terminal and it succeeds. In a Jupyter notebook, import pandas still fails with ModuleNotFoundError. Name the most likely cause and the first diagnostic step.
Show answer
The notebook's kernel uses a different Python interpreter than the one the terminal's pip belongs to, so pandas was installed into a site-packages directory the kernel does not read. First, run import sys; print(sys.executable) in a notebook cell to see the kernel's interpreter. Then install into that interpreter with python -m pip install pandas, using that same interpreter path.
Exercise
Project A needs pandas 1.5 and project B needs pandas 2.2. Explain why one shared global environment cannot serve both, and how virtual environments solve it.
Show answer
A site-packages directory holds only one installed version of each package, so installing pandas 2.2 for project B replaces version 1.5 that project A depends on. With a virtual environment per project, each has its own site-packages directory, so each can hold a different pandas version while sharing the same base interpreter.
Exercise
What does sys.prefix != sys.base_prefix tell you, and what value do you expect after activating a virtual environment?
Show answer
It tells you whether the running interpreter belongs to a virtual environment. Outside one the two paths are equal, so the expression is False. Inside an active virtual environment, sys.prefix points to the environment folder while sys.base_prefix points to the base installation, so the expression is True.