Stop 02 · Python for Machine LearningTopics · 5/7

Virtual Environments & pip/conda

Part of the Machine Learning Roadmap

In short

Project A needs one version of pandas, project B needs another, and installing both on one laptop breaks something. Virtual environments give each project its own private set of packages.

A virtual environment keeps each project's packages separate, so upgrading one library does not break another project. pip installs packages, conda also manages Python versions and native libraries, and requirements files let others recreate your setup.

The mascot packs dal, rice and kheer into separate steel tiffin compartments.

Think of it like this

Separate tiffin boxes

You don't pour dal, rice and kheer into one box, or everything tastes strange. Each goes in its own compartment. A virtual environment is a separate compartment of Python packages for each project, so nothing leaks into the others.

Watch it happen

Two projects, two environments

Your laptop
churn-project
old-thesis
.venv
.venv
pandas 2.x
scikit-learn
pandas 1.5

1/4One laptop, one system Python. Installing everything there causes version clashes.

The big ideas

What you need to know

One environment per project

Create a .venv inside each project folder and activate it before installing anything.

Pin your versions

pip freeze > requirements.txt records exactly what you installed so results can be reproduced.

Many tools, same idea

venv with pip, conda, and newer tools like uv all create isolated environments.

Don't commit the .venv

Add .venv to .gitignore. Share requirements.txt instead; others rebuild it in a minute.

See it in code

Walk through the code

bash
1# Create and activate a project environment2python3 -m venv .venv3source .venv/bin/activate        # Windows: .venv\Scripts\activate4 5pip install pandas scikit-learn6pip freeze > requirements.txt    # record exact versions7 8# On another laptop, or for a teammate9python3 -m venv .venv && source .venv/bin/activate10pip install -r requirements.txt

1/3Create a .venv folder and switch your terminal into it.

Your prompt usually shows (.venv) once it's active. Anything you pip install now stays inside this project.

Save yourself the pain

Common mistakes

Oops

Running pip install for every project into your system Python.

Do this instead

Activate the project's environment first. Check with 'which python' that it points inside .venv.

Oops

Sharing a project without a requirements file.

Do this instead

Always include requirements.txt (or your tool's lock file) so others get the same versions.

Your mission

Set up a clean project

~20 min

Quick check

Why commit requirements.txt but not the .venv folder?

Go deeper

  1. 1Create a venv for an ML project, install numpy, pandas and scikit-learn, and freeze them to requirements.txt.
  2. 2Try Miniconda and create an environment with a specific Python version.
  3. 3Delete the environment and rebuild it from requirements.txt to check it works.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge