Python venv Cheat Sheet

Comprehensive Python venv reference covering creation, activation, pip usage, requirements, inspection, and recovery workflows.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Create environments
Create a .venv environment
python -m venv .venv

# Create a conventional project-local virtual environment.

Create with python3
python3 -m venv .venv

# Use python3 explicitly on systems where python points elsewhere.

Create with Windows py launcher
py -m venv .venv

# Create an environment using the Windows Python launcher.

Create with a specific Python version
python3.12 -m venv .venv

# Use a specific interpreter to choose the base Python version.

Create in a custom directory
python -m venv ~/venvs/myproject

# Create a venv anywhere, not just in the project root.

Create without pip
python -m venv --without-pip .venv

# Skip pip bootstrap during environment creation.

Create with system site packages
python -m venv --system-site-packages .venv

# Allow access to packages installed in the base interpreter.

Recreate an existing environment in place
python -m venv --clear .venv

# Clear an environment directory before recreating it.

Upgrade an existing environment after Python upgrade
python -m venv --upgrade .venv

# Upgrade the environment after the base interpreter changed.

Customize the shell prompt name
python -m venv --prompt myapp .venv

# Set the prompt label shown when the environment is active.

Show venv help
python -m venv --help

# Display available venv command-line options.

## Activate and deactivate
Activate on bash/zsh
source .venv/bin/activate

# Activate a virtual environment on Linux or macOS.

Activate with dot shorthand
. .venv/bin/activate

# POSIX shell shorthand for source.

Activate in fish shell
source .venv/bin/activate.fish

# Use the fish shell activation script.

Activate in csh/tcsh
source .venv/bin/activate.csh

# Use the csh activation script.

Activate on Windows CMD
.venv\Scripts\activate.bat

# Activate from Command Prompt.

Activate on Windows PowerShell
.venv\Scripts\Activate.ps1

# Activate from PowerShell.

Allow local PowerShell activation scripts
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned

# Change policy so local activation scripts can run.

Deactivate environment
deactivate

# Leave the current virtual environment.

Check active python path
which python

# Verify the interpreter path after activation.

Check VIRTUAL_ENV variable
echo $VIRTUAL_ENV

# See which environment is active.

## Inspect the environment
Show active Python version
python --version

# Print the Python version from the current environment.

Show interpreter executable path
python -c "import sys; print(sys.executable)"

# Print the executable path for the active interpreter.

Show sys.prefix and sys.base_prefix
python -c "import sys; print(sys.prefix); print(sys.base_prefix)"

# Confirm whether you are inside a venv.

Show site-packages paths
python -c "import site; print(site.getsitepackages())"

# Print the active site-packages directories.

Show pip path and version
python -m pip --version

# Confirm pip belongs to the active environment.

List installed packages
python -m pip list

# Show packages currently installed in the environment.

Freeze package versions
python -m pip freeze

# Print installed packages in requirements format.

Show one package
python -m pip show requests

# Display metadata for an installed package.

Inspect pyvenv.cfg
cat .venv/pyvenv.cfg

# Review the environment metadata file.

## Daily package workflow
Upgrade pip tooling
python -m pip install --upgrade pip setuptools wheel

# Upgrade pip and common packaging helpers in the environment.

Install a package
python -m pip install requests

# Install one package in the active environment.

Install a pinned version
python -m pip install Django==5.1.4

# Install an exact package version.

Upgrade one package
python -m pip install --upgrade requests

# Upgrade a package to a newer version.

Uninstall a package
python -m pip uninstall requests

# Remove a package from the environment.

Install current project in editable mode
python -m pip install -e .

# Develop a local package without reinstalling after every change.

Install from requirements file
python -m pip install -r requirements.txt

# Install dependencies from a requirements file.

Write requirements.txt
python -m pip freeze > requirements.txt

# Snapshot the current environment to a requirements file.

Install using a constraints file
python -m pip install -r requirements.txt -c constraints.txt

# Apply version constraints during installation.

Check dependency consistency
python -m pip check

# Detect broken or incompatible installed dependencies.

Download wheels without installing
python -m pip download -r requirements.txt -d ./wheelhouse

# Fetch packages into a local directory for later use.

## Reset and rebuild
Delete a venv on Linux/macOS
rm -rf .venv

# Remove the environment directory completely.

Delete a venv on PowerShell
Remove-Item -Recurse -Force .venv

# Remove the environment directory recursively.

Recreate from requirements in one line
rm -rf .venv && python -m venv .venv && source .venv/bin/activate && python -m pip install -U pip setuptools wheel && python -m pip install -r requirements.txt

# Delete, recreate, and reinstall dependencies.

Typical Makefile rebuild target
venv:
  rm -rf .venv
  python -m venv .venv
  . .venv/bin/activate && python -m pip install -U pip setuptools wheel
  . .venv/bin/activate && python -m pip install -r requirements.txt

# Automate the rebuild workflow in a Makefile target.

Reinstall exactly from requirements
python -m pip install --upgrade --force-reinstall -r requirements.txt

# Force reinstall to match a requirements file closely.

## Bootstrap and repair pip
Bootstrap pip in current interpreter or venv
python -m ensurepip --default-pip

# Install bundled pip into the current Python environment.

Upgrade bundled pip bootstrap
python -m ensurepip --upgrade

# Upgrade pip using the bundled ensurepip package.

Create and upgrade core dependencies
python -m venv .venv && source .venv/bin/activate && python -m pip install --upgrade pip setuptools wheel

# Create a venv and upgrade pip tools immediately.

Prefer python -m pip
python -m pip --version

# Bind pip explicitly to the chosen interpreter.

Inspect Python resolution in PowerShell
Get-Command python

# See which python command PowerShell resolves.

Show all python executables on Windows
where python

# See multiple Python commands on PATH.

Recommended next

No recommendations yet.