Build garageTry it → read → next · ~9 min

Tutorials · Chapter D (4/4) · ~9 min

Python virtual environments

Try it → see it → read → next

Give each Python project its own package shelf so experiments stay reproducible.

Try yourself

Playground

Env conflict

Two projects need different package versions. A global install can’t please both.

Project A needs requests==2.28
Project B needs requests==2.32
Global install: requests==2.32 ← A breaks

Recap

What you just did

EnvConflictSim broke two projects with clashing packages, then fixed it with a venv per project.

Teach

How it works

From a project directory:

python -m venv .venv
source .venv/bin/activate
python -m pip install pandas
python -m pip freeze > requirements.txt

On Windows, activation is usually .venv\Scripts\activate.

  1. Create .venv with a specific Python
  2. Activate so python and pip point inside it
  3. Install only what this project needs
  4. Record dependencies so someone else can rebuild them

Mental model: each project gets a labeled toolbox instead of borrowing loose tools from one giant pile.

Use it

When you'd use this

  • Starting any data science or AI repository
  • Testing a package upgrade without breaking another project
  • Reproducing a teammate’s dependency list

Watch out

Watch out

Do not commit the .venv directory; it is large and machine-specific. Commit a dependency file instead. If an import still fails after installation, confirm your editor and terminal are using the environment’s Python.

Try next

Try this next

Deactivate the environment, check the Python path, reactivate, and check again. Explain why the paths differ.