Course topics

By WebNest Studio Engineering Team

Python Tutorial

Python Virtual Environments and Dependency Management

A virtual environment is an isolated Python installation that keeps one project's dependencies separate from every other project's dependencies, and separate from the system-wide Python installation. Without virtual environments, installing package versions for one project can silently break another project that needs a different, conflicting version of the same package — a problem often called "dependency hell."

Isolating dependencies per project also makes your work reproducible: anyone who clones your project can create an identical environment from a requirements file, rather than guessing which package versions you happened to have installed globally.

Why it matters

Two projects on one machine often need different versions of the same package, and installing everything into the system Python eventually breaks one of them. A virtual environment gives each project its own set of packages. Creating one is the first step in every real Python project, and it is assumed in every team and every deployment guide.

Creating and Activating a Virtual Environment with venv

Python includes the venv module in its standard library specifically for this purpose. Running python -m venv env creates a new directory named "env" containing a private copy of the Python interpreter and its own site-packages folder for installed libraries. You then "activate" it — on macOS/Linux with source env/bin/activate, on Windows with env\Scripts\activate — which changes your shell so that "python" and "pip" point to the environment's private copies instead of the system-wide ones. Deactivating with deactivate returns you to the system environment.

Installing Packages with pip

pip is Python's standard package installer, and it always installs into whichever Python environment is currently active. pip install requests downloads and installs the "requests" library and its dependencies from the Python Package Index (PyPI). pip install requests==2.31.0 pins an exact version, which matters for reproducibility since newer versions can introduce breaking changes.

requirements.txt

A requirements.txt file lists a project's dependencies, typically one package per line with an exact or minimum version, such as requests==2.31.0. Running pip freeze > requirements.txt captures everything currently installed in the active environment into this format, and pip install -r requirements.txt installs everything listed in the file in one command — this is exactly how a teammate or a deployment server recreates your environment reliably.

Example

Full workflow: create, activate, install, and freeze dependencies

Python
# Create a virtual environment named "env"
python -m venv env

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

# Activate it (Windows)
env\Scripts\activate

# Install project dependencies into the isolated environment
pip install requests flask

# Record exact installed versions for reproducibility
pip freeze > requirements.txt

# Later, on another machine, recreate the same environment:
pip install -r requirements.txt
Output
# requirements.txt contents after freeze, e.g.:
# flask==3.0.3
# requests==2.31.0
# (plus any transitive dependencies)

Common Mistakes

  • Installing packages globally with pip instead of inside an activated virtual environment, gradually polluting the system Python with unrelated project dependencies.
  • Forgetting to activate the virtual environment before running pip install, so packages end up installed in the wrong (usually system) interpreter.
  • Committing the entire virtual environment directory (e.g. "env/") to version control instead of just requirements.txt — this bloats the repository and is not portable across operating systems.
  • Never pinning versions in requirements.txt, so "pip install -r requirements.txt" installs different package versions over time and reproduces different behavior on different machines.

Exercise

Write the terminal commands, in order, to set up a new project: create a virtual environment named .venv, activate it, install the requests package, and save the list of installed packages to requirements.txt. These are shell commands, not Python code. Give the activation command for both Windows and macOS/Linux.

Python · starter code
# 1. create the virtual environment

# 2. activate it (Windows PowerShell)
#    activate it (macOS / Linux)

# 3. install requests

# 4. save the installed packages to requirements.txt
Show hints
  • The environment is created by the venv module, run with python -m.
  • pip freeze prints the installed packages with their versions; > sends that output to a file.
Show solution
Python · solution
# 1. create the virtual environment
python -m venv .venv

# 2. activate it (Windows PowerShell)
.venv\Scripts\Activate.ps1
#    activate it (macOS / Linux)
source .venv/bin/activate

# 3. install requests
pip install requests

# 4. save the installed packages to requirements.txt
pip freeze > requirements.txt

Quick quiz

1. What problem does a virtual environment solve?

  • A. Slow code
  • B. Running out of memory
  • C. Syntax errors
  • D. Projects needing different versions of the same package
Show answer

D. Each environment has its own installed packages.

2. Which command creates a virtual environment in the folder .venv?

  • A. python -m venv .venv
  • B. pip install venv
  • C. python create .venv
  • D. venv new .venv
Show answer

A. venv is part of the standard library.

3. Should the .venv folder be committed to Git?

  • A. Yes, always
  • B. No; commit requirements.txt and recreate the environment from it
  • C. Only on Windows
  • D. Only for small projects
Show answer

B. The folder is large and specific to one machine. It is added to .gitignore.

Interview questions

What is a virtual environment and how does it work?

Show answer

It is a folder containing a Python interpreter, or a link to one, and its own site-packages folder for installed packages. Activating it puts its folder first on the PATH, so python and pip refer to that environment. Packages installed while it is active go into it and do not affect other projects or the system Python.

How do you reproduce a project's environment on another machine?

Show answer

Record the dependencies with their versions in a file, traditionally requirements.txt written by pip freeze. On the other machine, create a new virtual environment, activate it and run pip install -r requirements.txt. Tools such as uv and Poetry do the same with a lock file that pins every package exactly.

Key Points to Remember

  • Virtual environments isolate each project's dependencies from the system Python and from each other.
  • python -m venv creates an environment; source env/bin/activate (or env\Scripts\activate on Windows) activates it.
  • pip installs into whichever environment is currently active, so activation order matters.
  • requirements.txt (built with pip freeze) makes a project's exact dependencies reproducible on any machine.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

These examples need a project environment on your computer, so they can be copied but not run here. Codelab runs Python, FastAPI backends with SQLite, plain Java 17 and HTML/CSS/JavaScript.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering