pip and Virtual Environments
You have a folder of bookshop sales and a plan to analyse it with pandas. Before any analysis, the tools have to be installed, and installed in a way that will not break the project you start next month. That is the job of pip and virtual environments.
The rule
One project, one environment; install inside it, record what you installed.
pip is Python's package installer. It downloads a library from the Python Package Index and puts it where Python can import it.
A virtual environment is a private folder holding its own Python and its own installed packages. Project A can keep pandas 2.1 while project B uses pandas 2.3, and neither notices the other.
python -m venv .venv # create the environment in a folder called .venv
source .venv/bin/activate # switch into it (Windows: .venv\Scripts\activate)
pip install pandas numpy matplotlib
pip freeze > requirements.txt # record exact versionsAnyone who gets your project later runs pip install -r requirements.txt inside their own fresh environment and gets the same versions.
Two other tools do the same job with a different style:
- conda manages environments and non-Python pieces (such as compiled maths libraries). Popular in scientific work.
- uv is a newer, very fast replacement for pip plus venv, driven by commands like
uv venvanduv pip install.
The idea is identical in all three: an isolated environment per project, plus a written list of what is in it.
The shortcut
Before typing any install command, ask "Which environment am I in?" Your terminal prompt usually shows it, like (.venv). No name showing means you are about to install into the system Python, which is shared by everything on the machine.
Make the environment
In the project folder, run
python -m venv .venv. A new.venvfolder appears.
A common trap: import pandas fails with ModuleNotFoundError even though you "installed it". Almost always, the install went into one environment and your code runs in another. Check which Python is running before you reinstall anything.
Drills
Answer fast — trust the rule.
What does a virtual environment give each project?
Which command records the exact package versions installed in the active environment?
Your prompt shows no environment name and you run pip install seaborn. Where does seaborn go?
A teammate needs your project's exact setup. What should they run inside their own fresh environment?
Remember
- One project, one environment; install inside it, record what you installed.
- Before installing, check the prompt: which environment am I in?
ModuleNotFoundErrorafter an install usually means two different environments, not a broken package.
