Software Config

Python Virtual Environments: Complete Guide for 2026

Tech Setup2 min read
TS

Tech Setup

Reviewed July 29, 2026

Why Virtual Environments Matter

Every Python project has its own dependencies. Without isolation, installing Package A v2.0 for one project breaks Project B that needs v1.0. Virtual environments solve this by giving each project its own Python and package directory.

Method 1: venv (Built-in)

Python 3.3+ ships with venv in the standard library. No extra install needed.

Create and activate

# Create environment
python -m venv .venv

# Activate (Windows)
.venv\Scripts\activate

# Activate (macOS/Linux)
source .venv/bin/activate

# Confirm you're isolated
which python   # should point to .venv
pip list       # only stdlib packages

Install dependencies

pip install flask sqlalchemy
pip freeze > requirements.txt

Reproduce on another machine

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Method 2: uv (Fast, Modern)

uv is a drop-in replacement for pip that's 10-100x faster.

# Install uv
pip install uv

# Create venv + install in one step
uv venv
uv pip install flask sqlalchemy

# Freeze
uv pip freeze > requirements.txt

Method 3: Poetry

Poetry manages virtual environments, dependencies, and publishing in one tool.

# Install Poetry
pip install poetry

# New project
poetry new my-project
cd my-project

# Add dependencies
poetry add flask
poetry add --group dev pytest

# Install all deps
poetry install

# Run commands inside the env
poetry run python main.py

pyproject.toml

Poetry generates a pyproject.toml that replaces setup.py and requirements.txt:

[tool.poetry]
name = "my-project"
version = "0.1.0"
python = "^3.11"

[tool.poetry.dependencies]
flask = "^3.0"

[tool.poetry.group.dev.dependencies]
pytest = "^8.0"

Method 4: Pipenv

pip install pipenv
pipenv install flask
pipenv shell

Pipenv creates a Pipfile and Pipfile.lock for reproducible installs.

Choosing Between Tools

ToolBest ForSpeed
venvSimple projects, scriptsFast
uvSpeed-critical workflowsFastest
PoetryLibrary/package developmentMedium
PipenvApplication developmentSlow

Common Mistakes

Forgetting to activate

# Wrong — installs globally
pip install flask

# Right — activate first
source .venv/bin/activate
pip install flask

Committing .venv to git

Add .venv to .gitignore:

.venv/
__pycache__/
*.pyc

Mixing system and venv packages

If pip list shows unexpected packages, you may have activated the wrong environment. Run which python to verify.

VS Code Integration

VS Code auto-detects .venv folders. If it doesn't:

  1. Open Command Palette (Ctrl+Shift+P)
  2. "Python: Select Interpreter"
  3. Choose the .venv Python executable

Docker + Virtual Environments

In Docker, you usually don't need venv since the container is already isolated:

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]

Quick Reference

TaskCommand
Createpython -m venv .venv
Activate (Win).venv\Scripts\activate
Activate (Unix)source .venv/bin/activate
Deactivatedeactivate
Freezepip freeze > requirements.txt
Install from filepip install -r requirements.txt
DeleteRemove the .venv folder