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.
# 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.
# 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 hereActivating and Deactivating
Always activate before working on a project.
# 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:
deactivateYour prompt shows (venv) when a virtual environment is active. If you don't see it, you're installing into the global Python.
Installing and Managing Packages
Install packages with pip after activating your venv.
# 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.txtNever Commit the venv Folder
Add venv/ to .gitignore. Teammates recreate it from requirements.txt.
# .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.txtVirtual 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/command | Purpose |
|---|---|
pip freeze > requirements.txt | record exact versions |
pip install -r requirements.txt | reproduce 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:
- Create a virtual environment with
python -m venv .venv. - Activate it and confirm
which python(orwhere python) points inside the project. - Install a package, then run
pip listto see it isolated from the global install. - Add
.venv/to a.gitignorefile.
🔥 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 .venvand activate it before installing dependencies. - Isolation prevents version conflicts between projects and keeps the global interpreter clean.
- Never commit the
.venvfolder — commitrequirements.txtinstead. - Reproduce an environment with
pip install -r requirements.txt. venvships with Python;condaandvirtualenvare 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
venvand tools likevirtualenvorconda? - How do you reproduce an environment on another machine?
- What does “activating” an environment actually change?
Related Topics
FAQ
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.
.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.
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.
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.

