Advertisement
🔧 Intermediate

Python Virtual Environments – Isolate Project Dependencies

A virtual environment is an isolated Python environment with its own interpreter and package library. Without one, all your projects share the same global Python installation — meaning a version conflict in one project can break others. Virtual environments solve this completely.

⏱️ 16 min read🎯 Intermediate📅 Updated 2026

Why Virtual Environments Matter

Without virtual environments: Project A needs requests 2.0, Project B needs requests 3.0 — they cannot coexist in global pip. Virtual environments give each project its own isolated space.

Python
# Without venv (global):
# pip install django==3.2  → breaks Project B which needs django==4.2

# With venv:
# project_a/venv/ → django 3.2 installed here
# project_b/venv/ → django 4.2 installed here
# Zero conflict!

Creating a Virtual Environment

Use Python's built-in venv module. Create one per project.

Python
# Create virtual environment (from project root)
python -m venv venv         # Windows
python3 -m venv venv        # macOS/Linux

# This creates:
# venv/
#   bin/python (or Scripts\python.exe on Windows)
#   bin/pip
#   lib/           ← installed packages go here

Activating and Deactivating

Always activate before working on a project.

Python
# Activate:
# Windows (Command Prompt):
venv\Scripts\activate

# Windows (PowerShell):
venv\Scripts\Activate.ps1

# macOS / Linux:
source venv/bin/activate

# Prompt changes to show (venv) prefix
# (venv) $ pip install requests   ← installs ONLY in this env

# Deactivate when done:
deactivate
💡
Tip

Your prompt shows (venv) when a virtual environment is active. If you don't see it, you're installing into the global Python.

Advertisement

Installing and Managing Packages

Install packages with pip after activating your venv.

Python
# Activate first, then:
pip install requests flask
pip install pandas==2.0.0    # specific version

# See what's installed:
pip list
pip show requests            # details about one package

# Save your dependencies:
pip freeze > requirements.txt

# Reproduce on another machine:
pip install -r requirements.txt

Never Commit the venv Folder

Add venv/ to .gitignore. Teammates recreate it from requirements.txt.

Python
# .gitignore
venv/
__pycache__/
*.pyc
.env
*.egg-info/
dist/
build/

# Teammate setup:
git clone your-repo
cd your-repo
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt

Virtual Environments: One Isolated Python per Project

A virtual environment is a private copy of Python and its packages for a single project. Without one, every pip install goes into your global Python — so Project A needing Django 3 and Project B needing Django 5 collide, and you can't have both.

python -m venv .venv            # create an env in ./.venv
source .venv/bin/activate       # activate (Linux/macOS)
# .venv\Scripts\activate       # Windows
pip install requests            # installs INTO this env only
deactivate                      # leave it

Once activated, python and pip point at the isolated env — installs and versions stay local to the project and never touch the system Python.

File/commandPurpose
pip freeze > requirements.txtrecord exact versions
pip install -r requirements.txtreproduce the env elsewhere

Best practices: one env per project, and never commit the .venv/ folder — add it to .gitignore and share requirements.txt instead so teammates rebuild it. This is what makes "works on my machine" reproducible: the dependency list travels with the code, the heavy installed files don't. Modern tools (poetry, uv, pipenv) automate the same idea.

🏋️ Practical Exercise

Master virtual environments:

  1. Create a virtual environment with python -m venv .venv.
  2. Activate it and confirm which python (or where python) points inside the project.
  3. Install a package, then run pip list to see it isolated from the global install.
  4. Add .venv/ to a .gitignore file.

🔥 Challenge Exercise

Set up two separate projects, each with its own virtual environment, and install different versions of the same package in each to prove isolation. Freeze each environment to its own requirements.txt, then delete and recreate one environment purely from its requirements file. Write a short note explaining why the .venv folder should never be committed to version control.

📋 Summary

  • A virtual environment is an isolated Python installation with its own packages.
  • Create one with python -m venv .venv and activate it before installing dependencies.
  • Isolation prevents version conflicts between projects and keeps the global interpreter clean.
  • Never commit the .venv folder — commit requirements.txt instead.
  • Reproduce an environment with pip install -r requirements.txt.
  • venv ships with Python; conda and virtualenv are alternatives with extra features.

Interview Questions on Virtual Environments

  • What is a virtual environment and why is it important?
  • How do you create and activate a virtual environment?
  • What problem does isolation solve?
  • Why should the virtual environment folder not be committed to git?
  • What is the difference between venv and tools like virtualenv or conda?
  • How do you reproduce an environment on another machine?
  • What does “activating” an environment actually change?

FAQ

Why should every project have its own virtual environment? +

Different projects often need different, sometimes conflicting, package versions. A per-project environment isolates dependencies so upgrading one project cannot break another, and keeps your global Python clean.

Why shouldn’t I commit the .venv folder to git? +

It is large, platform-specific, and fully reproducible from requirements.txt. Committing it bloats the repo and can break on other operating systems. Commit the requirements file instead and add .venv/ to .gitignore.

What does activating an environment do? +

It adjusts your shell’s PATH so python and pip resolve to the environment’s copies, scoping installs and imports to that project until you run deactivate.

What is the difference between venv and conda? +

venv is built into Python and manages Python packages via pip. conda is a separate package and environment manager that also handles non-Python dependencies, popular in data science.