Sheet ⁨04⁩ · ⁨Cheatsheets⁩Surveyed ⁨2026⁩
Cheatsheets

Python Virtual Environments Cheatsheet

A practical reference for venv, pip, requirements files, pyenv version switching, and pipx for isolated global tools.

5 Categories10 Sections21 ExamplesPublished: 17 Aug 2026Updated: 17 Aug 2026
Pythonvirtualenvpippyenvdependenciesrequirements.txtpip-toolspipx

A fast reference for keeping Python projects isolated and reproducible. It covers creating and activating environments with venv, installing packages with pip, locking dependencies with requirements files and pip-tools, switching interpreter versions with pyenv, and installing global CLI tools cleanly with pipx.

Creating and Activating Environments

Make an isolated environment per project so dependencies never collide.

Create a virtual environment

Use the built-in venv module to create an isolated environment in a project folder.

Create an environment named .venv

Creates a self-contained Python environment in the .venv folder, with its own interpreter and site-packages.

Code
Terminal window
python -m venv .venv
  • Name it .venv by convention so editors and tools auto-detect it.
  • Add .venv/ to your .gitignore; never commit an environment.

Create with a specific interpreter

Uses whichever Python you invoke as the base for the environment, which is how you pin the version.

Code
Terminal window
python3.12 -m venv .venv

Activate and deactivate

Activating puts the environment's interpreter and pip first on your PATH.

Activate on macOS or Linux

Your prompt gains a (.venv) prefix and python/pip now point inside the environment.

Code
Terminal window
source .venv/bin/activate

Activate on Windows

PowerShell activation. Use .venv\Scripts\activate.bat from cmd.exe instead.

Code
Terminal window
.venv\Scripts\Activate.ps1

Deactivate

Restores your original PATH. Works from any shell where the environment is active.

Code
Terminal window
deactivate

Remove an environment

Environments are disposable; delete and recreate rather than repairing.

Delete the environment

There is no uninstall command. The environment is just a folder, so removing it is the reset.

Code
Terminal window
rm -rf .venv

Installing Packages with pip

Install, upgrade, and inspect packages inside the active environment.

Install packages

pip installs into whichever environment is currently active.

Install a package

Code
Terminal window
pip install requests

Install a specific version

Quote version specifiers so the shell does not interpret the characters.

Code
Terminal window
pip install 'django==5.0.*'

Editable install of a local project

Links your source in place so code changes take effect without reinstalling. This is the usual setup while developing a package.

Code
Terminal window
pip install -e .

Inspect installed packages

See what is installed and whether anything is outdated.

List installed packages

Code
Terminal window
pip list

Show outdated packages

Code
Terminal window
pip list --outdated

Managing Dependencies

Capture and restore your dependency set so environments are reproducible.

requirements.txt

Freeze the current environment and restore it elsewhere.

Freeze current dependencies

Writes every installed package and its exact version so others can reproduce the environment.

Code
Terminal window
pip freeze > requirements.txt

Install from requirements.txt

Code
Terminal window
pip install -r requirements.txt

Deterministic installs with pip-tools

Separate the packages you ask for from the fully pinned set you install.

Compile a locked requirements file

Produces a fully pinned requirements.txt (including transitive deps and hashes) from your short requirements.in.

Code
Terminal window
# list only top-level deps in requirements.in, then:
pip-compile requirements.in

Sync the environment to the lockfile

Installs exactly what the lockfile says and removes anything that is not in it, so the environment matches the lock precisely.

Code
Terminal window
pip-sync requirements.txt

Switching Python Versions with pyenv

Install and switch between multiple Python versions per machine or per project.

Install and select versions

pyenv manages the Python interpreter itself, independently of your OS Python.

Install a Python version

Code
Terminal window
pyenv install 3.12.4

Set the global default

Code
Terminal window
pyenv global 3.12.4

Pin a version for one project

Writes a .python-version file so this directory always uses that interpreter.

Code
Terminal window
pyenv local 3.11.9

pyenv-virtualenv

Combine a pyenv-managed version with a named virtual environment.

Create a named environment on a chosen version

Creates a virtual environment tied to a specific pyenv Python, which you can then activate by name or auto-activate with pyenv local.

Code
Terminal window
pyenv virtualenv 3.12.4 myproject-3.12

Global CLI Tools with pipx

Install Python command-line tools globally without polluting any project environment.

Install and run tools

pipx gives each tool its own hidden environment while exposing its command on your PATH.

Install a CLI tool

Installs ruff in its own isolated environment but makes the ruff command available everywhere.

Code
Terminal window
pipx install ruff

Run a tool once without installing

Downloads into a temporary environment, runs the command, and cleans up. Handy for a tool you need once.

Code
Terminal window
pipx run cowsay moo

Was this useful?

You might also enjoy

More posts on similar topics

Git

Git is a distributed version control system. Developers use it to track code changes, collaborate with teams, and manage project history. Most software teams build their workflow around it. Key co

#Git#Version Control#Branches+5 tags
read more

Screen

Getting started GNU Screen is a terminal multiplexer that gives you:Sessions: Full terminal environments that persist even if you disconnect Windows: Multiple terminals (tabs) within

#Screen#Terminal Multiplexer#Sessions+2 tags
read more

Tmux

Tmux is a terminal multiplexer for managing multiple terminal sessions, windows, and panes within a single screen. This cheatsheet covers the commands, keybindings, and configuration options for every

#Tmux#Terminal Multiplexer#Sessions+3 tags
read more

Vim

Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as "vi" with most UNIX systems. The sections below cover Vim commands, sho

#Editor#Vim#Text Editor+3 tags
read more

VS Code

A quick reference for Visual Studio Code: keyboard shortcuts, editor features, and working habits for editing and navigation.

#VS Code#Visual Studio Code#Keyboard Shortcuts+3 tags
read more

Python

  • Mohammad Abu Mattar
  • Programming Language
  • Python
  • Scripting
  • Object Oriented
  • Web Development
  • Data Science

Python is an interpreted, high-level programming language known for its readability and simplicity. It supports multiple programming paradigms including procedural, object-oriented, and functional pro

#Python#Programming#Scripting+6 tags
read more

6 related posts