Python Virtual Environments

· 4 min read · Updated March 14, 2026 · beginner
python virtual-environments packaging pip

Virtual environments let you create isolated Python environments for each project. Instead of installing packages globally, you get a self-contained directory with its own Python interpreter and installed packages. This prevents version conflicts between projects and keeps your system Python clean.

Why Use Virtual Environments

When you install packages with pip, they go into your system’s Python site-packages directory. This works fine for simple scripts, but causes problems as your projects grow:

  • Version conflicts: Project A might need requests==2.28, while Project B needs requests==3.0. You cannot satisfy both with a single installation.
  • Polluted global environment: Test packages and development tools clutter your main Python installation.
  • Broken dependencies: Upgrading a package for one project might break another project that depends on a specific version.

Virtual environments solve all of these problems. Each project gets its own clean space with exactly the packages it needs.

Creating a Virtual Environment

Python’s built-in venv module creates virtual environments:

# Create a new virtual environment
python -m venv myenv

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

# Activate it (Windows)
myenv\Scripts\activate

After activating, your prompt changes to show the environment name. Any pip install now goes into this isolated environment, not the global Python.

Using Virtual Environments

Once activated, install packages normally:

# Install packages into the virtual environment
pip install requests
pip install flask==3.0.0

# List installed packages
pip list

# Freeze requirements for reproducibility
pip freeze > requirements.txt

The packages you install exist only in this virtual environment. Your global Python remains untouched.

Deactivating a Virtual Environment

When done working, deactivate to return to the global Python:

deactivate

The prompt returns to normal, and pip commands work with your system Python again.

Virtual Environments vs Virtualenv

Python includes venv as part of the standard library since Python 3.3. It provides the basic functionality most projects need.

The third-party virtualenv package offers additional features:

  • Creates environments with different Python versions
  • Symlinks system packages for faster creation
  • More configuration options

For most projects, venv is sufficient. Use virtualenv when you need its extra features:

# Install virtualenv
pip install virtualenv

# Create environment with specific Python
virtualenv --python=python3.11 myenv

Best Practices

One Environment Per Project

Create a new virtual environment for each project. This keeps dependencies isolated:

# Project directory
cd myproject

# Create environment in project folder
python -m venv .venv

# Activate
source .venv/bin/activate  # Linux/macOS
.venv\Scripts\activate     # Windows

Use a Consistent Location

Store virtual environments in a predictable place. Common options:

  • .venv inside each project (gitignored)
  • ~/venvs/ for a central collection

Activate Early in Your Workflow

Make activating the virtual environment the first step when working on a project. Add it to your shell profile or project documentation.

Check Requirements Files

After installing packages, export your dependencies:

pip freeze > requirements.txt

This file can be version-controlled and used to recreate the environment:

pip install -r requirements.txt

Visual Overview

Global Python (site-packages)
├── requests 2.28.0
├── flask 2.3.0
└── numpy 1.24.0

project-a/.venv (site-packages)
├── requests 2.28.0
└── flask 3.0.0    # Different version!

project-b/.venv (site-packages)
├── requests 3.0.0    # Even more different!
└── django 4.0.0

Each environment has its own independent set of packages. No conflicts.

Common Issues

Wrong Python Version

If you want a specific Python version, specify it explicitly:

# Use python3.11 instead of default
python3.11 -m venv myenv

Environment Not Activating

On Windows, ensure you are using the correct activate script:

# PowerShell
.venv\Scripts\Activate.ps1

# Command Prompt
.venv\Scripts\activate.bat

Packages Not Found

After activating, verify you are using the right pip:

which pip  # Linux/macOS
where pip  # Windows

It should point to your virtual environment’s pip, not the system one.

When to Recreate an Environment

Sometimes it’s faster to delete and recreate a virtual environment than to troubleshoot dependency issues:

  • Broken packages: If pip itself is corrupted, fixing it is harder than recreating
  • Unclear dependencies: When you lose track of what is installed, start fresh
  • Python version upgrade: When upgrading Python, best to create a new environment
  • Disk space issues: Old environments can accumulate; delete and recreate

To recreate:

# Remove the old environment
rm -rf .venv

# Create a fresh one
python -m venv .venv

# Activate and reinstall
source .venv/bin/activate
pip install -r requirements.txt

This approach gives you a clean slate with known dependencies.

Virtual Environments and IDEs

Most IDEs can detect and use virtual environments automatically:

  • VS Code: Select the interpreter from .venv/bin/python
  • PyCharm: Set the project interpreter to the virtual environment
  • PyDev: Configure the Python interpreter to point to .venv

This lets you run code within the IDE using the correct dependencies without manually activating the environment each time.

See Also