Cheatsheets / Python Environments Cheatsheet
Python Environments Cheatsheet
venv, pip, pyenv, Poetry, and uv - the commands for creating isolated Python environments and managing dependencies without breaking your system Python.
venv (standard library)
python3 -m venv .venv
Creates a virtual environment in a .venv folder, isolated from your system Python.
source .venv/bin/activate
Activates the environment on Linux/macOS - your shell prompt changes to show it’s active.
.venv\Scripts\Activate.ps1
Activates the environment on Windows PowerShell.
deactivate
Exits the active virtual environment, returning to your system Python.
which python
Confirms which python binary is currently active (should point inside .venv once activated).
pip
pip install requests
Installs a package into the currently active environment.
pip install requests==2.32.3
Installs an exact, pinned version.
pip install -r requirements.txt
Installs every package listed in a requirements file.
pip freeze > requirements.txt
Writes every currently installed package and its exact version to a requirements file.
pip list --outdated
Lists installed packages that have a newer version available.
pip install --upgrade requests
Upgrades a single package to its latest compatible version.
pip uninstall requests
Removes a package from the environment.
pip show requests
Shows a package’s version, location, and dependencies.
pip install -e .
Installs the current project in “editable” mode, so code changes take effect without reinstalling.
pipx (for CLI tools)
pipx install black
Installs a Python CLI tool into its own isolated environment, exposing only its command globally.
pipx list
Lists tools installed via pipx.
pipx upgrade black
Upgrades a pipx-installed tool.
pipx run black --check .
Runs a tool once without permanently installing it.
pyenv (multiple Python versions)
pyenv install 3.12.6
Downloads and builds a specific Python version.
pyenv versions
Lists Python versions installed via pyenv.
pyenv global 3.12.6
Sets the default Python version for your whole user account.
pyenv local 3.11.9
Pins a Python version for the current project, writing a .python-version file.
pyenv which python
Shows the full path to the Python binary pyenv would currently run.
Poetry
poetry init
Interactively creates a pyproject.toml for a new project.
poetry add requests
Adds a dependency and installs it into a Poetry-managed virtual environment.
poetry add --group dev pytest
Adds a dependency to a specific group (e.g. dev-only tools), keeping it out of production installs.
poetry install
Installs all dependencies listed in pyproject.toml/poetry.lock.
poetry remove requests
Removes a dependency from the project.
poetry run python app.py
Runs a command inside the project’s virtual environment without manually activating it.
poetry shell
Opens an interactive shell with the project’s virtual environment activated.
poetry update
Updates dependencies to the latest versions allowed by pyproject.toml, rewriting the lockfile.
poetry export -f requirements.txt -o requirements.txt
Exports a requirements.txt from poetry.lock, for tools that expect the plain-pip format.
uv (fast, modern tooling)
uv venv
Creates a virtual environment (in .venv by default), much faster than python -m venv.
uv pip install requests
Installs a package using pip-compatible syntax, but with uv’s much faster resolver.
uv pip sync requirements.txt
Installs exactly the packages listed, removing anything not in the file - reproducible installs.
uv init myproject
Scaffolds a new project with a pyproject.toml already set up.
uv add requests
Adds a dependency to the project and updates the lockfile, similar to poetry add.
uv run app.py
Runs a script inside the project’s environment, creating/syncing it automatically first if needed.
uv python install 3.12
Downloads and manages a specific Python version, without needing pyenv.
uv lock
Resolves and writes the project’s lockfile without installing anything.