100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Virtual Environments and pip in Python

Isolating project dependencies with venv and managing packages with pip -- creation, activation, and requirements files.

Concurrency & Interview PrepBeginner10 min readJul 7, 2026
Analogies

1. Introduction

Every real-world Python project depends on third-party packages, and different projects often need different (sometimes conflicting) versions of the same package. A virtual environment is an isolated Python installation with its own site-packages directory, so packages installed for one project never interfere with another project or with the system-wide Python. pip is Python's standard package installer, used to install, upgrade, and remove packages from PyPI (the Python Package Index).

🏏

Cricket analogy: Two franchise teams in the same league keep separate training kits so one team's new bat brand never ends up in the other's kit bag, just as a virtual environment keeps each project's packages isolated from others.

2. Syntax

bash
# Create a virtual environment named "venv"
python -m venv venv

# Activate it (Linux/macOS)
source venv/bin/activate

# Activate it (Windows)
venv\Scripts\activate

# Install a package
pip install requests

# Save dependencies
pip freeze > requirements.txt

# Install from a requirements file
pip install -r requirements.txt

# Deactivate
deactivate

3. Explanation

Isolating dependencies per project avoids version conflicts -- e.g., Project A needing Django 3.x while Project B needs Django 5.x on the same machine. Running python -m venv venv creates a self-contained directory holding a copy (or symlink) of the Python interpreter plus an empty site-packages folder. Activating the environment modifies your shell's PATH so that python and pip resolve to the venv's copies instead of the system-wide ones.

🏏

Cricket analogy: One club needing an older bat-weighing scale while another needs a newer digital one avoids conflict by each keeping its own gear; running python -m venv venv creates that dedicated equipment room, and 'activating' is like walking into that room so your tools are the ones you reach for.

Once active, pip install <package> installs packages only inside that environment. pip freeze lists every installed package with its exact version, which is typically redirected into a requirements.txt file so the exact dependency set can be reproduced elsewhere with pip install -r requirements.txt. Running deactivate restores the shell to using the system Python.

🏏

Cricket analogy: Once inside the dedicated equipment room, pip install <package> is like adding a new bat only to that room's inventory; pip freeze is the inventory list you export to a requirements.txt so another club can restock identically, and deactivate is walking back out to the shared gear.

A requirements.txt pinned with == versions (from pip freeze) ensures reproducible installs across machines, teammates' laptops, and CI pipelines. Never commit the venv/ directory itself to version control -- only commit requirements.txt (or a Pipfile/pyproject.toml) and let each environment be recreated locally.

4. Example

python
requirements_text = """flask==2.3.0
requests==2.31.0
numpy==1.26.0"""

packages = {}
for line in requirements_text.splitlines():
    name, version = line.split("==")
    packages[name] = version

for name, version in packages.items():
    print(f"{name} -> version {version}")

5. Output

text
flask -> version 2.3.0
requests -> version 2.31.0
numpy -> version 1.26.0

6. Key Takeaways

  • A virtual environment isolates a project's dependencies from the system Python and from other projects.
  • python -m venv venv creates the environment; source venv/bin/activate (or venv\Scripts\activate on Windows) activates it.
  • pip install <package> installs a package only inside the active environment.
  • pip freeze > requirements.txt records exact installed versions for reproducibility.
  • pip install -r requirements.txt recreates the same dependency set on another machine.
  • Never commit the venv/ folder to version control -- only requirements.txt.

Practice what you learned

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#VirtualEnvironmentsAndPipInPython#Virtual#Environments#Pip#Syntax#StudyNotes#SkillVeris