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 project-local or named virtual environments.

Create a .venv environment

Create a conventional project-local virtual environment.

bashANYcreatevenvpython
bash
python -m venv .venv

Creates a virtual environment in `.venv`. The Python docs recommend `venv` for isolated project environments, and `.venv` is a common convention. On Python, `venv` creates an isolated environment based on the interpreter you invoke.

Create with python3

Use python3 explicitly on systems where python points elsewhere.

bashANYcreatevenvpython3
bash
python3 -m venv .venv

Useful on Linux and macOS when `python3` is the installed interpreter command.

Create with Windows py launcher

Create an environment using the Windows Python launcher.

powershellWINDOWScreatewindowspy-launcher
powershell
py -m venv .venv

On Windows, `py` is often the most reliable way to target the installed Python interpreter.

Create with a specific Python version

Use a specific interpreter to choose the base Python version.

bashANYcreateversionpython3.12
bash
python3.12 -m venv .venv

A virtual environment uses the version of the Python binary that created it.

Create in a custom directory

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

bashANYcreatepathvenvs
bash
python -m venv ~/venvs/myproject

Useful if you prefer to keep environments outside the repository tree.

Create without pip

Skip pip bootstrap during environment creation.

bashANYcreatepipwithout-pip
bash
python -m venv --without-pip .venv

You can bootstrap pip later with `ensurepip` if you intentionally create a minimal environment.

Create with system site packages

Allow access to packages installed in the base interpreter.

bashANYcreatesystem-site-packagesisolation
bash
python -m venv --system-site-packages .venv

By default, venv environments are isolated. This option intentionally relaxes that isolation.

Recreate an existing environment in place

Clear an environment directory before recreating it.

bashANYcreateclearrecreate
bash
python -m venv --clear .venv

Useful when the directory exists and you want a fresh environment in the same path.

Upgrade an existing environment after Python upgrade

Upgrade the environment after the base interpreter changed.

bashANYupgradevenvpython
bash
python -m venv --upgrade .venv

Use this when the underlying Python installation was upgraded and you want the environment updated.

Customize the shell prompt name

Set the prompt label shown when the environment is active.

bashANYpromptvenvcreate
bash
python -m venv --prompt myapp .venv

Helpful when you manage many environments and want a clearer prompt name.

Show venv help

Display available venv command-line options.

bashANYhelpvenv
bash
python -m venv --help

Good starting point when you want to confirm supported flags on the interpreter you have installed.

Activate and deactivate

Enter and leave an environment across shells and OSes.

Activate on bash/zsh

Activate a virtual environment on Linux or macOS.

bashANYactivatebashzsh
bash
source .venv/bin/activate

After activation, `python` and `pip` resolve to the environment versions until you deactivate.

Activate with dot shorthand

POSIX shell shorthand for source.

bashANYactivatebashposix
bash
. .venv/bin/activate

Equivalent to `source` in POSIX-compatible shells.

Activate in fish shell

Use the fish shell activation script.

bashANYactivatefish
bash
source .venv/bin/activate.fish

Fish uses its own activation script instead of the bash one.

Activate in csh/tcsh

Use the csh activation script.

bashANYactivatecshtcsh
bash
source .venv/bin/activate.csh

Useful on systems where csh or tcsh is the active shell.

Activate on Windows CMD

Activate from Command Prompt.

batWINDOWSactivatewindowscmd
bat
.venv\Scripts\activate.bat

Use this in classic Command Prompt sessions.

Activate on Windows PowerShell

Activate from PowerShell.

powershellWINDOWSactivatewindowspowershell
powershell
.venv\Scripts\Activate.ps1

If execution policy blocks the script, temporarily allow local scripts for the session or user before activating.

Allow local PowerShell activation scripts

Change policy so local activation scripts can run.

powershellWINDOWSpowershellexecution-policywindows
powershell
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned

A common fix when PowerShell refuses to run `Activate.ps1`.

Deactivate environment

Leave the current virtual environment.

bashANYdeactivatevenv
bash
deactivate

Returns your shell to the base environment and resets PATH changes made by activation.

Check active python path

Verify the interpreter path after activation.

bashANYinspectactivatepython
bash
which python

On Windows PowerShell, use `Get-Command python` instead.

Check VIRTUAL_ENV variable

See which environment is active.

bashANYinspectenvironmentactivate
bash
echo $VIRTUAL_ENV

Activation scripts usually set `VIRTUAL_ENV` to the environment path.

Inspect the environment

Confirm interpreter paths, versions, site-packages, and config.

Show active Python version

Print the Python version from the current environment.

bashANYinspectpythonversion
bash
python --version

Helpful to confirm the active environment is using the expected interpreter version.

Show interpreter executable path

Print the executable path for the active interpreter.

bashANYinspectsys.executablepython
bash
python -c "import sys; print(sys.executable)"

A reliable way to verify exactly which Python binary is running.

Show sys.prefix and sys.base_prefix

Confirm whether you are inside a venv.

bashANYinspectsys.prefixvenv
bash
python -c "import sys; print(sys.prefix); print(sys.base_prefix)"

Inside a venv, `sys.prefix` usually points to the venv and `sys.base_prefix` to the base interpreter.

Show site-packages paths

Print the active site-packages directories.

bashANYinspectsite-packagespackages
bash
python -c "import site; print(site.getsitepackages())"

Useful when debugging where packages are actually installed.

Show pip path and version

Confirm pip belongs to the active environment.

bashANYinspectpippython-m-pip
bash
python -m pip --version

Using `python -m pip` is safer than a plain `pip` command because it binds pip to the interpreter you chose.

List installed packages

Show packages currently installed in the environment.

bashANYinspectpippackages
bash
python -m pip list

A basic inspection command for package inventory.

Freeze package versions

Print installed packages in requirements format.

bashANYfreezerequirementspip
bash
python -m pip freeze

Often used to produce reproducible dependency snapshots.

Show one package

Display metadata for an installed package.

bashANYinspectpipmetadata
bash
python -m pip show requests

Useful for checking install location, version, and dependencies.

Inspect pyvenv.cfg

Review the environment metadata file.

bashANYinspectpyvenv.cfgmetadata
bash
cat .venv/pyvenv.cfg

This file records key settings like the base home interpreter path and whether system site packages are visible.

Daily package workflow

Install, upgrade, remove, sync, and snapshot packages inside venv.

Upgrade pip tooling

Upgrade pip and common packaging helpers in the environment.

bashANYpipupgradetooling
bash
python -m pip install --upgrade pip setuptools wheel

A common first step after creating a fresh environment.

Install a package

Install one package in the active environment.

bashANYpipinstallpackage
bash
python -m pip install requests

Within an activated venv, this installs only into that environment by default.

Install a pinned version

Install an exact package version.

bashANYpipinstallversion-pin
bash
python -m pip install Django==5.1.4

Use exact pins when you need deterministic app dependencies.

Upgrade one package

Upgrade a package to a newer version.

bashANYpipupgradepackage
bash
python -m pip install --upgrade requests

Upgrades the package and resolves dependencies according to pip rules.

Uninstall a package

Remove a package from the environment.

bashANYpipuninstallpackage
bash
python -m pip uninstall requests

Useful for cleanup and dependency debugging.

Install current project in editable mode

Develop a local package without reinstalling after every change.

bashANYpipeditabledevelopment
bash
python -m pip install -e .

Common for local application and library development.

Install from requirements file

Install dependencies from a requirements file.

bashANYrequirementspipinstall
bash
python -m pip install -r requirements.txt

A standard way to set up project dependencies from source control.

Write requirements.txt

Snapshot the current environment to a requirements file.

bashANYfreezerequirementssnapshot
bash
python -m pip freeze > requirements.txt

Creates a plain-text dependency snapshot in pip-compatible format.

Install using a constraints file

Apply version constraints during installation.

bashANYconstraintspiprequirements
bash
python -m pip install -r requirements.txt -c constraints.txt

Helpful when several apps share dependency limits.

Check dependency consistency

Detect broken or incompatible installed dependencies.

bashANYpipcheckdependencies
bash
python -m pip check

Useful when installs succeeded but runtime imports still seem wrong.

Download wheels without installing

Fetch packages into a local directory for later use.

bashANYpipdownloadoffline
bash
python -m pip download -r requirements.txt -d ./wheelhouse

Useful for offline or controlled deployment workflows.

Reset and rebuild

Delete and recreate environments cleanly when they drift.

Delete a venv on Linux/macOS

Remove the environment directory completely.

bashANYdeleterebuildunix
bash
rm -rf .venv

The simplest reset is often to delete the environment and recreate it from your requirements or lock source.

Delete a venv on PowerShell

Remove the environment directory recursively.

powershellWINDOWSdeleterebuildwindows
powershell
Remove-Item -Recurse -Force .venv

PowerShell equivalent of deleting the whole environment folder.

Recreate from requirements in one line

Delete, recreate, and reinstall dependencies.

bashANYrebuildrequirementsvenv
bash
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

A compact rebuild flow for local development on POSIX shells.

Typical Makefile rebuild target

Automate the rebuild workflow in a Makefile target.

makefileANYmakefileautomationrebuild
makefile
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

Useful for standardizing the setup process across a team.

Reinstall exactly from requirements

Force reinstall to match a requirements file closely.

bashANYpipforce-reinstallrequirements
bash
python -m pip install --upgrade --force-reinstall -r requirements.txt

Not a perfect lock sync, but often useful when you want to refresh packages from a known file.

Bootstrap and repair pip

Recover when pip or activation breaks.

Bootstrap pip in current interpreter or venv

Install bundled pip into the current Python environment.

bashANYensurepippipbootstrap
bash
python -m ensurepip --default-pip

The Python standard library includes `ensurepip` to bootstrap pip when it is missing.

Upgrade bundled pip bootstrap

Upgrade pip using the bundled ensurepip package.

bashANYensurepippipupgrade
bash
python -m ensurepip --upgrade

Useful when pip is missing or inconsistent after a broken environment change.

Create and upgrade core dependencies

Create a venv and upgrade pip tools immediately.

bashANYbootstrapvenvpip
bash
python -m venv .venv && source .venv/bin/activate && python -m pip install --upgrade pip setuptools wheel

A very common bootstrap pattern for fresh projects.

Prefer python -m pip

Bind pip explicitly to the chosen interpreter.

bashANYpython-m-pippipbest-practice
bash
python -m pip --version

This is safer than relying on a standalone `pip` executable that may resolve to a different Python installation.

Inspect Python resolution in PowerShell

See which python command PowerShell resolves.

powershellWINDOWSinspectpowershellwindows
powershell
Get-Command python

Useful when activation appears to work but PowerShell still resolves the wrong interpreter.

Show all python executables on Windows

See multiple Python commands on PATH.

batWINDOWSwindowspathinspect
bat
where python

Helpful when several Python installations are competing on PATH.

Recommended next

No recommendations yet.