Virtual Environments
Virtual environments are the foundation of every reliable Python project. The handbook explains what a venv is, why you need one, and how to manage Python interpreters and isolated environments with modern tools.
Concepts
Tools
Migration paths
More Virtual Environments pages
Everything else tagged virtual-environments, grouped by section. Pages featured above are not repeated here.
How To
How to Cache uv Dependencies in CI
Cache uv's package store and managed interpreters in CI to skip re-downloads, and keep .venv out so stale packages never survive a restore.
How to configure Claude Code to use virtual environments
Make Claude Code use virtual environments via `uv run`, CLAUDE.md rules, env config, or PreToolUse hooks. Shell activation doesn't persist.
How to create and use a Python virtual environment with venv
Create and activate a Python virtual environment with venv on macOS, Linux, and Windows. Includes activation syntax and troubleshooting.
How to customize uv's virtual environment location
Override uv's default .venv location using UV_PROJECT_ENVIRONMENT environment variable or the --active flag to use an existing virtual environment.
How to Fix "zsh: command not found: python" on macOS
Fix zsh: command not found: python on macOS by running python3 for a quick check or installing uv to manage Python and put it on PATH.
How to Fix ModuleNotFoundError: No Module Named 'package'
Find which of five causes broke your import in one command, so the package you installed finally imports.
How to Fix pyenv's "No module named '_lzma'" Error
Fix a pyenv Python missing the lzma module by installing xz and rebuilding the interpreter, or skip source compilation with uv.
How to fix the "externally-managed-environment" error
Fix the externally-managed-environment error on Linux. Use virtual environments or uv tool install instead.
How to Inspect a Virtual Environment You Did Not Create
Read the dependency tree of an existing virtualenv with uv pip tree to learn what is installed and which package pulled it in.
How to Install Python (and Which Method to Choose)
Install Python with uv on macOS, Windows, or Linux, and let one tool manage versions so every project gets the right one.
How to Install Python on macOS
Install Python on macOS with uv instead of the system python3 or Homebrew, so pip stops failing and versions stay current.
How to Install Python on Windows
Install Python on Windows with uv or python.org. Stop the Microsoft Store from hijacking python and get a real interpreter on your PATH for development.
How to Migrate from conda to uv
Convert a conda environment to a uv project with pyproject.toml and uv.lock for reproducible installs that finish in seconds.
How to require a virtualenv when installing packages with pip?
Configure pip to refuse package installation outside a virtual environment using PIP_REQUIRE_VIRTUALENV.
How to use pip in a uv virtual environment
Add pip to a uv virtual environment. Support Jupyter %pip magic and tools that require direct pip access.
How to use uv on NixOS
Install uv on NixOS, fix the dynamic-linker error that breaks managed Python downloads, and pin uv per project with a flake.nix dev shell.
Explanation
Do I need a project to use uv?
uv works without a project directory. Here are options for using uv with single-file scripts, script collections, and when each approach makes sense.
Should I use Homebrew to install Python?
Homebrew's Python exists to support other Homebrew tools and can break unexpectedly on upgrade. Use uv to manage Python instead.
What is a .python-version file?
.python-version: specify your project's Python version. Tools like uv and pyenv auto-switch when you cd into the directory.
What is PEP 668?
PEP 668 marks system Python installations as externally managed, causing pip to refuse package installation outside a virtual environment.
Reference
direnv
direnv: loads/unloads environment variables per directory. Activate Python venvs and manage project settings without global pollution.
mise
mise is a cross-platform CLI that manages multiple language runtimes, environment variables, and tasks per project from a single mise.toml manifest.
pipdeptree
pipdeptree renders installed packages as a dependency tree, revealing which package pulled in each transitive dependency.
pyenv-virtualenv
pyenv-virtualenv is a pyenv plugin for creating and auto-activating virtual environments tied to specific Python versions.
From the blog
Posts tagged virtual-environments.
-
uv 0.8 Release: Automatic Python Installation to PATH
uv 0.8 adds automatic Python executable installation to PATH, making uv-managed Python versions available system-wide.
-
Managing Python Versions In Your uv Projects
How .python-version files, requires-python, and uv work together to manage Python versions in your projects.
-
Why Use uv Projects Instead of requirements.txt?
Comparing requirements.txt and pyproject.toml approaches to Python dependency management, and why uv projects offer more.
-
Flexible Python Version Management with uv and tox
Solve tox Python version mismatch errors by using uvx to run tox with any Python version on demand.
-
Require pip to install packages in virtual environment
Configure pip to only install packages inside virtual environments using the global.require-virtualenv setting.
-
Quick start guide for Python development on a Mac
A quick start guide for setting up Python on macOS using pyenv and Poetry, with a note on the modern uv alternative.