---
title: "Python Virtual Environments Cheatsheet"
lang: "en"
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/python-venv
---

[Home](/)›[Cheatsheets](/cheatsheets)

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

[Markdown for AI(opens in a new tab)](/python-venv/index.md "Open the plain-Markdown version of this page, for pasting into an AI tool")

Series

[Developer Environment & Tooling](/series/developer-environment--tooling)6/6

[PreviousVS Code](/cheatsheets/vscode)

All posts in this series (6)

Cheatsheets6

1.  [Git](/cheatsheets/git)
2.  [Screen](/cheatsheets/screen)
3.  [Tmux](/cheatsheets/tmux)
4.  [Vim](/cheatsheets/vim)
5.  [VS Code](/cheatsheets/vscode)
6.  [Python Virtual Environments CheatsheetYou are here](/cheatsheets/python-venv)

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](#category-virtualenvs)

-   [Create a virtual environment](#section-create-venv)
-   [Activate and deactivate](#section-activate-venv)
-   [Remove an environment](#section-remove-venv)

[Installing Packages with pip](#category-pip)

-   [Install packages](#section-pip-install)
-   [Inspect installed packages](#section-pip-inspect)

[Managing Dependencies](#category-dependencies)

-   [requirements.txt](#section-requirements)
-   [Deterministic installs with pip-tools](#section-pip-tools)

[Switching Python Versions with pyenv](#category-pyenv)

-   [Install and select versions](#section-pyenv-install)
-   [pyenv-virtualenv](#section-pyenv-virtualenv)

[Global CLI Tools with pipx](#category-pipx)

-   [Install and run tools](#section-pipx-install)

No commands found

Try adjusting your search term

## 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.

#### Keywords

venvcreatevirtualenv

#### 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.

#### Best Practices

-   Confirm activation with \`which python\` (it should point inside .venv).
-   You do not have to activate: \`.venv/bin/python script.py\` works directly, which is handy in scripts and CI.

#### Keywords

activatedeactivatesource

#### 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

```
1.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.

#### Common Errors

-   **pip installs globally instead of into the environment:** You forgot to activate. Check \`which pip\`, then re-run after activating .venv.

#### Keywords

pipinstallupgrade

#### 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.

#### Best Practices

-   pip freeze captures everything, including transitive dependencies, which makes it noisy. Keep your true top-level deps in a separate file.

#### Keywords

requirements.txtfreezereproducible

#### 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.

#### Advanced Notes

-   **Why pip-compile over plain freeze:** requirements.in records intent (the packages you actually want), while the compiled output records the exact resolved graph. Editing intent and recompiling is far cleaner than hand-editing a frozen dump.

#### Keywords

pip-toolspip-compilelockfile

#### 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.

#### Keywords

pyenvversionsgloballocal

#### 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.

#### Best Practices

-   Use pipx for tools (black, ruff, httpie, poetry), and venv for project libraries. Never pip install CLI tools into your system Python.

#### Keywords

pipxglobalisolation

#### 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?

## Tags

#Python#Virtualenv#Pip#Pyenv#Dependencies#Requirements.txt#Pip tools#Pipx

## Share

[Facebook](https://facebook.com/sharer/sharer.php?u=https%3A%2F%2Fmkabumattar.com%2Fcheatsheets%2Fpython-venv "Share on Facebook")[Twitter](https://twitter.com/intent/tweet/?text=Python%20Virtual%20Environments%20Cheatsheet&url=https%3A%2F%2Fmkabumattar.com%2Fcheatsheets%2Fpython-venv "Share on Twitter")[LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fmkabumattar.com%2Fcheatsheets%2Fpython-venv&title=Python%20Virtual%20Environments%20Cheatsheet&summary=A%20practical%20reference%20for%20venv%2C%20pip%2C%20requirements%20files%2C%20pyenv%20version%20switching%2C%20and%20pipx%20for%20isolated%20global%20tools.&source=https://mkabumattar.com "Share on LinkedIn")[WhatsApp](https://wa.me/?text=Python%20Virtual%20Environments%20Cheatsheet%20https%3A%2F%2Fmkabumattar.com%2Fcheatsheets%2Fpython-venv "Share on WhatsApp")[Telegram](https://t.me/share/url?url=https%3A%2F%2Fmkabumattar.com%2Fcheatsheets%2Fpython-venv&text=Python%20Virtual%20Environments%20Cheatsheet "Share on Telegram")[Reddit](https://www.reddit.com/submit?url=https%3A%2F%2Fmkabumattar.com%2Fcheatsheets%2Fpython-venv&title=Python%20Virtual%20Environments%20Cheatsheet "Share on Reddit")[Hacker News](http://news.ycombinator.com/submitlink?u=https%3A%2F%2Fmkabumattar.com%2Fcheatsheets%2Fpython-venv&t=Python%20Virtual%20Environments%20Cheatsheet "Share on Hacker News")[Pinterest](https://pinterest.com/pin/create/button/?url=https%3A%2F%2Fmkabumattar.com%2Fcheatsheets%2Fpython-venv&media=&description=A%20practical%20reference%20for%20venv%2C%20pip%2C%20requirements%20files%2C%20pyenv%20version%20switching%2C%20and%20pipx%20for%20isolated%20global%20tools. "Share on Pinterest")[Email](<mailto:?subject=Python%20Virtual%20Environments%20Cheatsheet&body=Check out this article: https%3A%2F%2Fmkabumattar.com%2Fcheatsheets%2Fpython-venv>)

## Comments

## You might also enjoy

More posts on similar topics

## [Git](/cheatsheets/git)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   Version Control
-   Git
-   SCM
-   Collaboration
-   Development Tools

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](/cheatsheets/git)

## [Screen](/cheatsheets/screen)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   Terminal
-   Screen
-   Tools
-   Development
-   System Administration

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](/cheatsheets/screen)

## [Tmux](/cheatsheets/tmux)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   Terminal
-   Tmux
-   Tools
-   Development
-   System Administration

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](/cheatsheets/tmux)

## [Vim](/cheatsheets/vim)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   Editor
-   Text Editor
-   Terminal
-   Command Line
-   Productivity

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](/cheatsheets/vim)

## [VS Code](/cheatsheets/vscode)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   Editor
-   Text Editor
-   Developer Tools
-   Productivity
-   Programming
-   Shortcuts

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](/cheatsheets/vscode)

## [Python](/cheatsheets/python)

-   [Mohammad Abu Mattar](/authors/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](/cheatsheets/python)

6 related posts
