PYTHON • GETTING STARTED AND TOOLING

Virtual Environments (venv) — Understand virtual environments conceptually (venv) and why they are used

Learn how Python isolates project dependencies so your code stays clean, portable, and conflict-free.

Historical Context & Motivation

When Python first became popular in the late 1990s and early 2000s, most developers installed packages directly into a single, system-wide Python installation. This approach worked fine when you had only one or two small scripts, but as developers began building larger projects that depended on many third-party libraries, a serious problem emerged: dependency conflicts. If Project A needed version 1.0 of a library and Project B needed version 2.0 of the same library, you were stuck — only one version could be installed at a time.

The Python community recognized this pain point and started building tools to create isolated environments for each project. Over the years, these tools evolved from community-driven add-ons into an official part of the Python language itself. Today, the built-in venv module makes it easy for anyone — even a first-time coder — to create a clean, isolated workspace for every project.

2007
virtualenv Released
Ian Bicking created virtualenv, the first widely adopted tool for creating isolated Python environments. It proved the concept was essential for real-world development.
2011
pip Becomes Standard
pip replaced easy_install as the default package installer. With pip, installing third-party libraries became simple — but managing versions across projects remained a challenge.
2012
PEP 405 Accepted
Python Enhancement Proposal 405 proposed adding a built-in virtual environment module to Python's standard library, making environment isolation a first-class feature of the language.
2014
venv Ships with Python 3.4
The venv module officially shipped with Python 3.4, giving every Python installation a built-in way to create virtual environments without installing anything extra.

The core question that drove this history is simple: How can a developer work on multiple Python projects on the same computer without those projects interfering with each other? Virtual environments are the answer.

Core Principles & Definitions

Before diving into commands and code, it is important to understand the key ideas behind virtual environments. A virtual environment is a self-contained directory that holds its own copy of the Python interpreter and its own set of installed packages. When you activate a virtual environment, any packages you install go into that directory rather than into your system's global Python installation.

1

Isolation

Each virtual environment is independent. Installing, upgrading, or removing a package in one environment has zero effect on other environments or on the system Python.
2

Reproducibility

You can freeze the exact list of packages and versions used in your project into a requirements.txt file, so anyone else can recreate the same environment perfectly.
3

Lightweight

Virtual environments do not copy the entire Python installation. They use symbolic links or small configuration files to point back to the base interpreter, keeping them fast to create and small in size.
4

Disposable

Because they are just directories, virtual environments can be deleted and recreated at any time. If something goes wrong, you simply delete the folder and start fresh.
✦ KEY TAKEAWAY
Think of a virtual environment like a separate backpack for each class at school. Your math backpack has a calculator and graph paper; your art backpack has brushes and paints. You would never want paint leaking onto your math homework. By keeping supplies separate, each class runs smoothly. A virtual environment does the same thing for your Python projects — each project gets its own "backpack" of packages.

Visual Explanation

The diagram below shows the difference between a system without virtual environments (left) and a system that uses them (right). Notice how, on the left, all projects share the same pool of packages. On the right, each project gets its own isolated set of packages.

On the left, Project A and Project B share packages — leading to version conflicts. On the right, each project has its own virtual environment with exactly the packages it needs.

The key insight from this diagram is that virtual environments act as protective boundaries. Each project sees only the packages installed inside its own environment. Project A can use requests 2.28 while Project B simultaneously uses requests 2.31 — and neither project is aware of or affected by the other.

How Virtual Environments Work Under the Hood

When you run python -m venv myenv, Python creates a new directory called myenv that contains a specific folder structure. This directory does not copy the entire Python installation — that would waste disk space. Instead, it creates a lightweight configuration that tells Python: "When I am active, look for packages here, not in the system location."

The Folder Structure

Inside the myenv directory, you will find three important parts. The bin (or Scripts) folder contains a copy or symbolic link to the Python interpreter, along with the activate script that switches your terminal to use this environment. The lib folder (specifically lib/pythonX.Y/site-packages) is where all packages installed via pip are stored. Finally, the pyvenv.cfg file is a small configuration file that records the path to the base Python interpreter and other settings.

The PATH Trick

When you activate a virtual environment, the activate script modifies a system variable called PATH. The PATH variable is a list of directories your terminal searches through when you type a command. By putting the virtual environment's bin folder at the front of PATH, the terminal finds the environment's Python interpreter before the system's one. This means any python or pip command you run will use the virtual environment's versions automatically.

The folder structure of a virtual environment showing the bin/ (executables), lib/ (packages), and pyvenv.cfg (configuration). The bottom shows how activation prepends the environment's path.

The Virtual Environment Workflow

Using virtual environments follows a clear, repeatable workflow. Every time you start a new Python project, you should create and activate a virtual environment before installing any packages. Below is a breakdown of the typical steps, along with the commands for each major operating system.

Standard venv workflow commands
StepmacOS / Linux CommandWindows Command
1. Createpython3 -m venv myenvpython -m venv myenv
2. Activatesource myenv/bin/activatemyenv\Scripts\activate
3. Install packagespip install requestspip install requests
4. Freeze depspip freeze > requirements.txtpip freeze > requirements.txt
5. Deactivatedeactivatedeactivate
💡 Pro Tip
Never commit your virtual environment folder to Git or other version control systems. Instead, add myenv/ to your .gitignore file. Share the requirements.txt file instead, so others can recreate the environment with pip install -r requirements.txt.

When you see your terminal prompt prefixed with something like (myenv), that means the virtual environment is active. Any pip install command you run will place packages inside that environment. When you type deactivate, your terminal returns to using the system Python.

Worked Example: Setting Up a Web Scraping Project

Let's walk through a realistic scenario. Imagine you want to build a simple web scraper that downloads data from a website. You need two external libraries: requests for making HTTP requests and beautifulsoup4 for parsing HTML. Here is how you would set up your project from scratch using a virtual environment.

Creating a Virtual Environment for a Web Scraper
1
Step 1 — Create the Project FolderOpen your terminal and navigate to where you keep your projects. Create a new directory and move into it. mkdir web-scraper && cd web-scraper
2
Step 2 — Create the Virtual EnvironmentRun the venv module to create an isolated environment inside a folder called venv. python3 -m venv venv
A new venv/ directory appears inside your project folder.
3
Step 3 — Activate the EnvironmentOn macOS/Linux, run source venv/bin/activate. On Windows, run venv\Scripts\activate. Your prompt changes to show (venv) at the beginning, confirming the environment is active.
Terminal shows: (venv) user@computer:~/web-scraper$
4
Step 4 — Install the Required PackagesWith the environment active, use pip to install your dependencies. These packages go into venv/lib/python3.x/site-packages/, not into your system Python. pip install requests beautifulsoup4
Both packages and their dependencies are installed inside the virtual environment only.
5
Step 5 — Freeze DependenciesRecord the exact versions so anyone can recreate your environment later. pip freeze > requirements.txt
The requirements.txt file now contains lines like requests==2.31.0 and beautifulsoup4==4.12.2.
6
Step 6 — Deactivate When DoneWhen you finish working on this project, simply type deactivate to return to the system Python. Your project's packages remain safely inside the venv/ folder, ready for next time.

Strengths, Limitations & Alternatives

The built-in venv module is the recommended starting point for virtual environment management, but it is not the only tool available. Understanding its strengths and limitations will help you decide when to use it and when to explore alternatives.

venv Strengths vs. Limitations
FeatureStrength ✓Limitation ✗
Built-inShips with Python 3.3+; nothing extra to install.Not available for Python 2 (which is end-of-life anyway).
SpeedCreates environments in seconds because it uses symlinks.Slightly slower than some third-party tools on very large projects.
SimplicityOnly a few commands to learn — easy for beginners.Does not manage Python versions itself (only packages).
IsolationFull package isolation per project prevents conflicts.Does not isolate non-Python system dependencies (e.g., C libraries).
Portabilityrequirements.txt makes sharing easy.The venv folder itself is not portable between machines or OS.

Alternative Tools

  • virtualenv — The original third-party tool. Faster creation, supports older Python versions, more features.
  • conda — Manages both Python versions and non-Python dependencies. Popular in data science.
  • Poetry — Combines dependency management and packaging into one tool with automatic virtual environment creation.
  • pipenv — Merges pip and virtualenv into a single workflow with a Pipfile for tracking dependencies.
✦ KEY TAKEAWAY
For a high school student getting started with Python, venv is the ideal choice because it requires no extra installation and teaches you the fundamental concepts. As your projects grow more complex, tools like conda or Poetry build on the same principles.

Connection to Advanced Development Practices

Virtual environments are just the beginning of a larger concept in software development: environment isolation. Professional developers use even more powerful isolation tools, and understanding venv gives you a solid foundation for learning them.

From venv to Professional Workflows
Conceptvenv (What You Know Now)Advanced Version
Package isolationvenv isolates Python packages per project.Docker containers isolate the entire operating system environment.
Dependency trackingrequirements.txt lists exact package versions.Lock files (Poetry's poetry.lock, Pipfile.lock) include hashes for security.
Python versionvenv uses whichever Python you run the command with.Tools like pyenv let you install and switch between multiple Python versions.
AutomationManual creation and activation.CI/CD pipelines (GitHub Actions, etc.) auto-create environments and run tests.

As you progress in your computer science journey, you will encounter concepts like containerization (Docker), continuous integration (CI/CD), and infrastructure as code. All of these build on the same principle that venv teaches: keep environments reproducible and isolated so that software behaves the same everywhere it runs.

Practice Problems

PROBLEM 1 — CONCEPTUAL
In your own words, explain what a virtual environment is and give one reason why a Python developer would use one instead of installing packages globally.
PROBLEM 2 — BASIC CALCULATION
Write the exact sequence of terminal commands you would run on macOS or Linux to: (a) create a virtual environment called env, (b) activate it, and (c) install a package called flask.
PROBLEM 3 — INTERMEDIATE
Your friend sends you their Python project as a zip file. Inside the zip, you find a requirements.txt file and several .py files, but no venv/ folder. Explain why the venv folder is missing and describe the steps you would take to get the project running on your own machine.
PROBLEM 4 — APPLIED
You are working on two class projects. Project A is a data analysis assignment that requires pandas 1.5 and matplotlib 3.6. Project B is a web app that requires pandas 2.0 and flask 2.3. Describe how you would organize your computer to work on both projects without conflicts, including the folder structure and commands.
PROBLEM 5 — CRITICAL THINKING
A classmate argues: "Virtual environments are a waste of time. I just install everything globally and it works fine." Present at least three counterarguments explaining scenarios where this approach would fail or cause problems, and explain how virtual environments prevent each issue.

Lesson Summary

A virtual environment is an isolated directory containing its own Python interpreter and packages, created using the built-in venv module with the command python -m venv <name>. They solve the fundamental problem of dependency conflicts by ensuring each project has its own independent set of packages. The core workflow is: create → activate → install → freeze → deactivate.

When activated, the environment works by modifying the PATH variable so the terminal finds the environment's Python before the system's. The requirements.txt file captures exact package versions for reproducibility, allowing collaborators to recreate the same setup. Virtual environments are lightweight and disposable — delete the folder and start over anytime. This concept is the foundation for advanced environment isolation practices like containerization with Docker.

Varsity Tutors • Python • Virtual Environments (venv)