pip and venv commands cheat sheet
Checking Python and pip
-
python3 --versionShows the Python version installed on the server.
-
python3 -m pip --versionShows the pip version and the Python version it belongs to. Running pip as python3 -m pip runs the pip of the interpreter you chose, so packages are installed for exactly that Python, even when several versions are installed on the server. All the commands on this page use this form.
-
which python3Shows the path of the interpreter that will run. When a virtual environment is active, the path ends with .venv/bin/python3.
Creating a virtual environment
-
python3 -m venv .venvCreates a virtual environment in the .venv directory inside the project directory, with its own Python interpreter and pip. Packages installed in it don't affect the system Python or other projects. Don't keep the project code inside the .venv directory, and don't commit the directory to the code repository.
-
sudo apt install python3-venvOn Debian and Ubuntu, the venv module comes in a separate package. When creating the environment fails, install the package and run python3 -m venv again.
-
python3 -m venv --upgrade-deps .venvCreates the environment and immediately upgrades the pip inside it to the latest version.
-
python3 -m venv --system-site-packages .venvCreates an environment that also has access to the packages installed in the system Python. The default is a fully isolated environment.
-
python3 -m venv --clear .venvCaution: deletes all the contents of the existing environment and creates it again, so all the packages installed in it are deleted. You install them again from the requirements.txt file.
-
rm -rf .venv && python3 -m venv .venvCaution: deletes the environment completely and creates it again. The deletion can't be undone, so run the command only from the project directory, and only after you save the package list to requirements.txt. A virtual environment can't be moved or copied: when you move a project to another directory or another server, you create a new environment there.
Activating and deactivating the environment
-
source .venv/bin/activateActivates the environment on Linux and macOS. The environment name appears at the start of the command prompt, and the python and pip commands now use the environment.
-
.venv\Scripts\activateActivates the environment on Windows, in the cmd command prompt.
-
.venv\Scripts\Activate.ps1Activates the environment on Windows, in PowerShell.
-
deactivateExits the active environment and returns to the system Python. Closing the terminal window also ends the activation.
-
.venv/bin/python app.pyRuns the script with the environment's interpreter, without activating the environment. Commands installed in the environment are also in the .venv/bin directory and run from there directly.
-
.venv/bin/python -m pip install requestsInstalls a package into the environment without activating it, for example from a deployment script.
Installing, updating, and removing packages
-
python3 -m pip install requestsInstalls the latest version of the package from PyPI, together with its dependencies. Run the command while the virtual environment is active.
-
python3 -m pip install "requests==2.31.0"Installs an exact version of the package.
-
python3 -m pip install "requests>=2.0,<3.0"Installs the latest version within the range you set. The quotes are required, because without them the shell interprets the < and > characters as file redirection.
-
python3 -m pip install -U requestsUpgrades an installed package to the latest version. Without -U, installing an existing package again changes nothing. The dependencies are upgraded only when the new version requires it.
-
python3 -m pip install --dry-run requestsShows what would be installed, without actually installing anything.
-
python3 -m pip install .Installs the project in the current directory, based on its pyproject.toml file.
-
python3 -m pip install -e .Installs the project in development mode (editable): code changes take effect immediately, without reinstalling.
-
python3 -m pip uninstall requestsRemoves the package after asking for confirmation.
-
python3 -m pip uninstall -y requestsRemoves the package without asking for confirmation, for example in a script.
-
python3 -m pip install --upgrade pipUpgrades pip itself. Run it while the virtual environment is active, so the upgrade applies only to the environment's pip.
-
python3 -m pip install --user requestsInstalls the package for the current user only, rather than for all users on the server. Inside a virtual environment, pip rejects this option, and on a server it's better to install packages in a virtual environment.
Information about installed packages
-
python3 -m pip listShows all the packages installed in the environment, with their versions.
-
python3 -m pip list --outdatedShows only packages that have a newer version, with the installed version and the latest version.
-
python3 -m pip list --not-requiredShows only packages that no other installed package depends on, in other words, the packages you installed directly.
-
python3 -m pip show requestsShows details about the package: version, description, installation location, and the packages it depends on.
-
python3 -m pip show -f requestsAlso shows the list of all the files the package installed.
-
python3 -m pip checkChecks that the dependencies of all the installed packages exist and have compatible versions. When everything is fine, the message No broken requirements found appears.
Requirements and constraints files
-
python3 -m pip freeze > requirements.txtSaves all the packages installed in the environment, with their exact versions, to the requirements.txt file. You keep the file in the code repository.
-
python3 -m pip install -r requirements.txtInstalls all the packages in the file, in the versions saved in it. This way, you create an environment on the server that's identical to the development environment.
-
python3 -m pip install -r requirements.txt -c constraints.txtInstalls the packages in requirements.txt, and sets their versions and the versions of their dependencies according to constraints.txt. A package that appears only in the constraints file isn't installed because of it.
-
python3 -m pip uninstall -r requirements.txt -yRemoves all the packages listed in the file, without asking for confirmation.
Cache and installing without internet access
-
python3 -m pip cache dirShows the pip cache directory. On Linux, the default is ~/.cache/pip.
-
python3 -m pip cache infoShows information about the cache, including how much space it takes up.
-
python3 -m pip cache purgeDeletes all the files from the cache and frees up disk space. The next installations download the packages again.
-
python3 -m pip install --no-cache-dir -r requirements.txtInstalls without using the cache. The installation is slower and downloads more data, so use this option only when you have a reason to.
-
python3 -m pip download -d ./packages -r requirements.txtDownloads the packages in the file, with their dependencies, to the packages directory, without installing them.
-
python3 -m pip install --no-index --find-links=./packages -r requirements.txtInstalls the packages only from the local directory, without contacting PyPI. Suitable for a server without internet access.
The externally-managed-environment error
-
error: externally-managed-environmentThe error appears on recent Linux distributions when you run pip install outside a virtual environment. The distribution marks the system Python as managed by its own package manager (PEP 668), so that pip doesn't break system components that depend on it. Inside a virtual environment, the error doesn't appear.
-
python3 -m venv .venv && .venv/bin/python -m pip install requestsThe right fix for an application or a script: create a virtual environment and install the packages in it.
-
python3 -m pip install --break-system-packages requestsCaution: lets pip change the system Python despite the marking. The installation can break distribution tools that depend on Python, so don't use it on a server. Install in a virtual environment or with pipx instead.
pipx for command-line tools
-
sudo apt install pipxInstalls pipx on Ubuntu and Debian. On Fedora, you install it with sudo dnf install pipx. The tool installs each Python application in its own separate virtual environment, and lets you run it from any directory.
-
pipx ensurepathAdds the directory where pipx keeps the applications to the PATH variable. After running it, open a new terminal window.
-
pipx install blackInstalls a command-line tool, in this example black, in its own isolated environment, without sudo and without affecting the system Python.
-
pipx listShows the applications installed with pipx.
-
pipx upgrade blackUpgrades one application to the latest version.
-
pipx upgrade-allUpgrades all the applications installed with pipx.
-
pipx uninstall blackRemoves the application and deletes its environment.
-
pipx run black --versionDownloads the latest version of the application to a temporary environment and runs it, without a permanent installation.
uv: installation, environments, and packages
-
curl -LsSf https://astral.sh/uv/install.sh | shInstalls uv on Linux and macOS with the official installation script. uv is a tool for managing packages, virtual environments, and Python versions, with commands similar to those of pip and venv. You can also install it with pipx install uv.
-
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"Installs uv on Windows, in PowerShell.
-
uv self updateUpdates uv to the latest version. The command works only when uv was installed with the official installation script. When it was installed with pipx, you update it with pipx upgrade uv.
-
uv venvCreates a virtual environment in the .venv directory inside the current directory. You activate it just like a venv environment, with source .venv/bin/activate.
-
uv venv --python 3.12Creates a virtual environment with a specific Python version. When that version isn't installed, uv can download it.
-
uv pip install requestsInstalls a package through the pip-compatible interface. You don't need to activate the environment: uv installs into the active environment, and when no environment is active, into the .venv environment in the current directory or in one of its parent directories.
-
uv pip install -r requirements.txtInstalls all the packages listed in the requirements.txt file.
-
uv pip compile requirements.in -o requirements.txtCreates a requirements.txt file with the exact versions of all the packages and their dependencies, based on the package list in the requirements.in file.
-
uv pip sync requirements.txtMakes the environment match the file exactly: installs the packages listed in it and removes the packages that aren't listed in it.
-
uv pip freezeShows the packages installed in the environment, in requirements.txt format.
-
uv python install 3.12Installs a Python version managed by uv. Without a version number, the command installs the latest version. The uv python list command shows the available versions and the versions that are already installed.
uv in a project: uv.lock and uv sync
-
uv init myappCreates a new project in the myapp directory, with a pyproject.toml file.
-
uv add requestsAdds a package to the project dependencies in the pyproject.toml file, and updates the uv.lock lockfile and the project environment.
-
uv remove requestsRemoves a package from the project dependencies.
-
uv lockUpdates the uv.lock lockfile, which stores the exact versions of all the dependencies. Don't edit the file by hand, and keep it in the code repository.
-
uv lock --checkChecks that the uv.lock file is up to date, without changing it.
-
uv lock --upgrade-package requestsUpgrades one package in the lockfile to the latest version that meets the project's requirements, and leaves all the other versions unchanged.
-
uv syncInstalls the packages from the uv.lock file into the project environment, and removes packages that aren't listed in it. This way, you create an environment on the server that's identical to the development environment.
-
uv sync --no-devInstalls the dependencies without the dev dependency group, for example on a production server.
-
uv run main.pyRuns a script in the project environment. Before running it, uv checks that the lockfile and the environment are up to date.
pyenv for managing Python versions
-
curl -fsSL https://pyenv.run | bashInstalls pyenv with the official installation script. Before you install Python versions, install the build packages that the pyenv documentation lists for each distribution. pyenv doesn't run on Windows, except inside WSL.
-
git clone https://github.com/pyenv/pyenv.git ~/.pyenvAn alternative way to install pyenv, from its code repository, into the ~/.pyenv directory.
-
export PYENV_ROOT="$HOME/.pyenv"The first of three lines you add to the ~/.bashrc file so that pyenv works in bash. This line sets the pyenv installation directory.
-
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"The second line in the ~/.bashrc file: adds the pyenv command to the PATH variable.
-
eval "$(pyenv init - bash)"The third line in the ~/.bashrc file: enables pyenv in every terminal window. After you add the three lines, open a new terminal window.
-
pyenv install -lShows all the versions you can install.
-
pyenv install 3.12Installs the latest known version in the series you specify, in this example 3.12.
-
pyenv versionsShows all the installed versions, with an asterisk next to the active version.
-
pyenv local 3.12Sets the version for the current directory and the directories inside it, through the .python-version file. Then you create the virtual environment in that directory with python -m venv .venv.
-
pyenv global 3.12Sets the default version for all of the user's terminal windows. The system Python stays unchanged.
-
pyenv shell 3.12Sets the version for the current terminal window only.
-
pyenv which pythonShows the full path of the interpreter that pyenv runs.
-
pyenv uninstall 3.12.8Caution: removes an installed Python version. Virtual environments created with this version stop working.
Running from a systemd service and from cron
-
WorkingDirectory=/home/deploy/appA line in the systemd service file, in the [Service] section: sets the working directory of the application, in this example the project directory.
-
ExecStart=/home/deploy/app/.venv/bin/python /home/deploy/app/main.pyRuns the application with the interpreter of the virtual environment, by its full path. You don't need the source command, because the environment's interpreter uses the environment's packages even without activation.
-
*/15 * * * * /home/deploy/app/.venv/bin/python /home/deploy/app/job.pyA crontab line that runs a script every 15 minutes with the environment's interpreter. In cron, always use full paths, because cron doesn't load the environment settings of your terminal window.
No matching commands.
Create a separate virtual environment for each Python project, and install the packages the project needs in it. This way, each project has its own package versions, and updating one project doesn't break another project or the system tools. You save the package list to requirements.txt with python3 -m pip freeze > requirements.txt, and on the server you create a new environment and install from the file with python3 -m pip install -r requirements.txt. In many projects, the dependencies are also defined in the pyproject.toml file, in the dependencies list of the [project] table.
On a production server, you run the application as a systemd service, so it starts again after a crash and when the server boots. In the service file, you write the full path to the environment's interpreter, for example /home/deploy/app/.venv/bin/python, and you don't need to activate the environment. After you create or change the service file, run sudo systemctl daemon-reload. The commands for managing the service are on the systemctl commands page.
On a Python VPS, the environment comes ready: Python, pip, and venv are preinstalled and configured, and you get full root access over SSH, so you create a virtual environment and install your packages right away. On a Linux VPS, you connect over SSH with full root access, install Python, and run every command on this page.
pip and venv commands FAQ
Why create a virtual environment for each Python project?
A virtual environment is a directory with its own Python interpreter and packages. When each project has a separate environment, each project gets exactly the versions it needs, and updating a package in one project doesn't affect another project or the system Python. You create it with python3 -m venv .venv and activate it with source .venv/bin/activate.
What does the externally-managed-environment error mean, and how do I fix it?
Recent Linux distributions mark the system Python as managed by the distribution's package manager, so pip refuses to install packages into it. The fix is to install the packages in the project's virtual environment, where the error doesn't appear. You install command-line tools, such as black, with pipx, which automatically creates a separate environment for each tool.
How do I move a Python project to a new server with the same packages?
On the current server, save the package list with python3 -m pip freeze > requirements.txt. On the new server, copy the code and the file, create a new virtual environment, and run python3 -m pip install -r requirements.txt. You don't copy the .venv directory itself, because a virtual environment can't be moved, so you create it again in every location.
Do I need to activate the virtual environment to run an application from a service or from cron?
No. It's enough to run the application with the environment's interpreter by its full path, for example /home/deploy/app/.venv/bin/python /home/deploy/app/main.py, and it uses the environment's packages automatically. That's how you write the ExecStart line in a systemd service file and the command in a crontab line.