Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Setting Up Your Data Toolkit  ›  Lesson

pip and Virtual Environments

virtualenv / venv 12 minEach project gets its own virtual environment, packages are installed into it with pip, and the exact versions are recorded so the setup can be rebuilt.

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 versions

Anyone 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 venv and uv 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.

Starting the bookshop analysis projectStep 1 of 4
  1. Make the environment

    In the project folder, run python -m venv .venv. A new .venv folder 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.

quick drill

What does a virtual environment give each project?

quick drill

Which command records the exact package versions installed in the active environment?

quick drill

Your prompt shows no environment name and you run pip install seaborn. Where does seaborn go?

quick drill

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?
  • ModuleNotFoundError after an install usually means two different environments, not a broken package.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy not install everything once?

A friend installs pandas into their system Python for every project. Explain what can go wrong, and describe the steps you would follow instead for a new analysis project.