Question
Python Virtual Environments Explained: venv vs virtualenv vs pyenv vs pipenv
Question
Python 3.3 added the venv module to the standard library. What does venv do, and how is it different from related Python tools such as pyvenv, pyenv, virtualenv, virtualenvwrapper, and pipenv?
In other words, these tools all seem to be related to Python environments, version management, or dependency isolation. When should each one be used, and what problem does each tool solve?
Short Answer
By the end of this page, you will understand what a Python virtual environment is, what venv does, and how it differs from tools like virtualenv, pyenv, virtualenvwrapper, and pipenv. You will also learn when each tool is useful, how they can work together, and which tool is the simplest choice for most beginner projects.
Concept
A virtual environment in Python is an isolated folder that contains its own Python executable and installed packages. Its main purpose is to let one project use its own dependencies without affecting other projects on the same machine.
For example:
- Project A may need
Django 4.x - Project B may need
Flask 3.x - Project C may need an older library version for compatibility
Without isolation, installing packages globally can cause version conflicts.
What venv does
venv is the built-in standard library tool for creating virtual environments in Python 3. It creates a self-contained environment for a specific project.
Example:
python -m venv .venv
This creates a directory named .venv containing:
- a Python interpreter
- a place for installed packages
- activation scripts
After activation, commands like python and pip use that environment instead of the global Python installation.
Why there are many similar tools
Different tools solve different layers of the same general problem:
venv: create isolated environments
Mental Model
Think of Python development like cooking in different kitchens.
- Global Python is one big shared kitchen in your house.
- A virtual environment is a separate kitchen counter for one recipe, with only the tools and ingredients that recipe needs.
venv/virtualenvbuild that separate counter.pyenvdecides which house or kitchen model you are using, such as Python 3.10 or Python 3.12.virtualenvwrapperis like labeling and organizing all your kitchen counters so you can switch between them easily.pipenvis like a kit that tries to manage both the counter and the shopping list for ingredients.
So:
- If you need package isolation, use a virtual environment tool.
- If you need different Python versions, use a Python version manager.
- If you want dependency files plus automatic environment creation, use a workflow tool like
pipenv.
Syntax and Examples
Create a virtual environment with venv
python -m venv .venv
Activate it:
macOS / Linux
source .venv/bin/activate
Windows
.venv\Scripts\activate
Install a package:
pip install requests
Check where Python is coming from:
python -c "import sys; print(sys.executable)"
This should point to the Python executable inside .venv.
Equivalent idea with virtualenv
virtualenv .venv
source .venv/bin/activate
virtualenv does roughly the same job as venv: it creates an isolated environment.
Using pyenv
Step by Step Execution
Consider this sequence:
python -m venv .venv
source .venv/bin/activate
pip install requests
python -c "import requests; print(requests.__version__)"
What happens step by step
1. python -m venv .venv
Python runs the built-in venv module.
It creates a new folder named .venv containing:
- a Python executable
- site-packages for installed libraries
- scripts for activation
2. source .venv/bin/activate
Your shell updates environment variables so that when you type python or pip, the versions inside .venv are used first.
This usually changes your prompt to show the environment name.
3. pip install requests
pip installs requests into the .venv environment, not into the global Python installation.
4.
Real World Use Cases
1. Keeping project dependencies separate
A web API may need one set of packages, while a data analysis script needs another. Virtual environments prevent package conflicts.
2. Working on multiple projects with different package versions
You might maintain:
- one project using
Django 5 - another using
Django 4
Each project can have its own environment.
3. Testing code against different Python versions
Use pyenv to install Python 3.10, 3.11, and 3.12, then create separate environments for testing.
4. Team collaboration
A project can include a dependency file, and each developer creates their own isolated environment locally.
5. Safe experimentation
If you want to try a new package without affecting your system Python, create a temporary virtual environment first.
Real Codebase Usage
In real projects, developers often combine tools rather than using only one.
Common patterns
Pattern 1: pyenv + venv
A very common setup:
- Use
pyenvto choose the Python version for the project - Use
venvto isolate project dependencies
Example:
pyenv local 3.12.3
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
This is a clean and popular workflow.
Pattern 2: virtualenvwrapper for many environments
If you manage many environments, virtualenvwrapper adds helper commands like:
mkvirtualenvworkonrmvirtualenv
This is about convenience, not a different isolation model.
Pattern 3: pipenv for dependency tracking
Some teams prefer pipenv because it manages:
Common Mistakes
1. Confusing Python version management with virtual environments
Beginners often think pyenv replaces venv.
It does not.
pyenvmanages which Python version is usedvenvmanages isolated packages for a project
2. Installing packages globally by accident
Broken workflow:
pip install requests
If no environment is active, this may install globally.
Better:
python -m venv .venv
source .venv/bin/activate
python -m pip install requests
Using python -m pip helps ensure you use the pip tied to the active interpreter.
3. Committing the virtual environment to Git
Do not commit .venv/ or other environment folders.
Add this to .gitignore:
.venv/
4. Assuming is the recommended modern tool
Comparisons
| Tool | Main purpose | Built in? | Manages Python versions? | Manages virtual environments? | Manages dependencies? |
|---|---|---|---|---|---|
venv | Create isolated environments | Yes | No | Yes | No |
pyvenv | Older command for venv | Was bundled historically | No | Yes | No |
virtualenv | Create isolated environments | No | No | Yes | No |
Cheat Sheet
Quick definitions
venv: built-in Python 3 module for virtual environmentspyvenv: older command related tovenv; generally avoid using it nowvirtualenv: third-party virtual environment toolvirtualenvwrapper: convenience layer for managing many virtualenvspyenv: install and switch Python versionspipenv: dependency manager that also creates virtual environments
Most common commands
Create env with venv
python -m venv .venv
Activate on macOS/Linux
source .venv/bin/activate
Activate on Windows
.venv\Scripts\activate
Deactivate
deactivate
Install packages into current env
FAQ
What is the difference between venv and virtualenv?
venv is the standard library solution in Python 3, while virtualenv is a third-party package that also creates virtual environments and historically supported more scenarios.
Is pyenv the same as a virtual environment?
No. pyenv manages Python versions installed on your machine. It does not mainly exist to isolate project packages.
Should I use venv or pipenv?
If you want the simplest standard approach, use venv. If you want a tool that also manages dependency files and lock files, pipenv may be useful.
Is pyvenv deprecated?
In modern practice, python -m venv is the preferred command. pyvenv is mostly an older command and is generally not the recommended modern choice.
Can I use pyenv and venv together?
Yes. This is a common setup: use to pick the Python version, then use to isolate dependencies for the project.
Mini Project
Description
Create a small Python project setup script and workflow that demonstrates the difference between selecting a Python interpreter and creating an isolated environment. This project helps you practice using venv correctly and understand where installed packages go.
Goal
Set up a project-local virtual environment, install a package into it, verify that the environment is isolated, and record dependencies.
Requirements
- Create a new project folder for a small Python script.
- Create a virtual environment named
.venvusingvenv. - Activate the environment and install the
requestspackage. - Write a Python script that imports
requestsand prints its version. - Save the installed dependencies to
requirements.txt.
Keep learning
Related questions
@staticmethod vs @classmethod in Python Explained
Learn the difference between @staticmethod and @classmethod in Python with clear examples, use cases, mistakes, and a mini project.
Call a Function by Name in a Python Module
Learn how to call a function by name in a Python module using strings, getattr, and safe patterns for dynamic function dispatch.
Catch Multiple Exceptions in One except Block in Python
Learn how to catch multiple exceptions in one Python except block using tuples, with examples, mistakes, and real-world usage.