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
# 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
deactivate3. 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
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
flask -> version 2.3.0
requests -> version 2.31.0
numpy -> version 1.26.06. Key Takeaways
- A virtual environment isolates a project's dependencies from the system Python and from other projects.
python -m venv venvcreates 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.txtrecords exact installed versions for reproducibility.pip install -r requirements.txtrecreates the same dependency set on another machine.- Never commit the venv/ folder to version control -- only requirements.txt.
Practice what you learned
1. What is the main purpose of a Python virtual environment?
2. Which command creates a virtual environment named 'venv' using the standard library?
3. Which file is conventionally used to record a project's pinned dependencies?
4. Which command generates a list of installed packages with exact versions?
5. What should you typically NOT commit to version control?
6. After activating a virtual environment, running `pip install requests` will install the package...
Was this page helpful?
You May Also Like
Modules and Packages in Python
Learn how Python organizes code into reusable modules and packages, and how the import system locates and caches them.
os Module in Python
Explore Python's os module for interacting with the operating system, including file paths, directories, and environment information.
Installing Python and IDE Setup
A practical walkthrough of installing Python 3, verifying the installation, and configuring a code editor or IDE for productive development.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics