Virtual environments & packages
Almost every real Python project uses packages from PyPI, and every project needs its own versions of them. A virtual environment is the standard way to keep those packages separate per project, and on Ubuntu 24.04 it's no longer optional: plain pip install outside one is refused.
Why virtual environments exist
Without one, pip install puts packages into the Python that came with the operating system. That causes three problems:
- Version clashes: project A needs
requests2.28, project B needs 2.32. One Python can only have one version installed. - Breaking the OS: Ubuntu's own tools are written in Python and installed with
apt. Upgrading a library under them with pip can break them. - No record: nobody knows which packages the project actually needs, so it won't run on the next machine.
A virtual environment (venv) is a folder containing its own python, its own pip and its own site-packages folder where installs go. Each project gets one, usually called .venv inside the project.
Create, activate, deactivate
$ cd ~/projects/monitor
$ python3 -m venv .venv # create it (once per project)
$ source .venv/bin/activate # Linux / macOS
(.venv) $ which python
/home/demee/projects/monitor/.venv/bin/python
(.venv) $ python -m pip install requests
(.venv) $ deactivate # back to the system Python
On Windows the folder is called Scripts instead of bin:
C:\monitor> py -m venv .venv
C:\monitor> .venv\Scripts\activate <- cmd.exe
PS C:\monitor> .venv\Scripts\Activate.ps1 <- PowerShell
If PowerShell says running scripts is disabled, allow it for your user once with Set-ExecutionPolicy -Scope CurrentUser RemoteSigned.
"Activating" does nothing magic: it puts .venv/bin at the front of your PATH (see the Linux chapter on environment) and adds (.venv) to the prompt. That also means you can skip activation and call the venv's Python directly, which is exactly what you do in cron jobs, systemd services and Dockerfiles:
$ /home/demee/projects/monitor/.venv/bin/python check.py
Add .venv/ to .gitignore: it's big, contains machine-specific paths and can always be recreated. For the same reason you can't move or rename a venv folder; delete it and create a new one.
pip: installing and inspecting
(.venv) $ pip install requests # latest version
(.venv) $ pip install "requests==2.32.3" # exact version
(.venv) $ pip install "requests>=2.31,<3" # any version in a range
(.venv) $ pip install --upgrade requests # newer version of an installed package
(.venv) $ pip uninstall requests
(.venv) $ pip list # everything installed
(.venv) $ pip show requests # version, location, dependencies
Quote version specifiers: an unquoted > or < is shell redirection. python -m pip is the same as pip but guarantees you're using the pip that belongs to that exact python, which avoids a whole class of "I installed it but it's not found" problems.
requirements.txt: recording what the project needs
(.venv) $ pip freeze > requirements.txt
(.venv) $ cat requirements.txt
certifi==2026.7.22
charset-normalizer==3.5.1
idna==3.20
PyYAML==6.0.2
requests==2.32.3
urllib3==2.8.0
# on another machine, or in CI / a Dockerfile:
$ python3 -m venv .venv && source .venv/bin/activate
(.venv) $ pip install -r requirements.txt
pip freeze lists every installed package with its exact version, including dependencies you never asked for (certifi, idna and friends come with requests). Your version numbers will differ from these. Pinning exact versions (==) means the server gets exactly what you tested; without pins, a new release published tomorrow can break a deploy that worked today. You can also write the file by hand with only your direct dependencies:
# requirements.txt
requests~=2.32.0 # 2.32.x only: bug fixes yes, new minor version no
PyYAML==6.0.2
# comments and blank lines are allowed
Tools like Poetry and uv add lock files and speed on top of this, but they're built on the same ideas: one isolated environment per project and a recorded list of versions.
pipx: Python command-line tools
Some packages are really applications you want as a command everywhere (linters, formatters, httpie), not libraries for one project. pipx installs each one into its own hidden venv and puts just the command on your PATH:
$ sudo apt install pipx
$ pipx ensurepath # adds ~/.local/bin to PATH (open a new terminal after)
$ pipx install black
$ black --version
$ pipx list # what's installed
$ pipx upgrade black
$ pipx uninstall black
Errors you will meet (and how to fix them)
externally-managed-environment on Ubuntu 23.04+ and Debian 12+, when you pip install outside a venv:
$ pip install requests
error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to
install.
...
This is deliberate (PEP 668): the system Python belongs to apt. Fixes, best first: use a venv; for a library a system tool needs, sudo apt install python3-requests; for a command-line app, pipx install. The flag --break-system-packages exists, but its name is an honest description of what it can do.
No venv module on a fresh Ubuntu:
$ python3 -m venv .venv
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt install python3.12-venv
...
$ sudo apt install python3-venv # installs the right python3.X-venv, then create the venv again
Similarly, pip: command not found or python: command not found outside a venv is normal on Ubuntu 24.04: the command is python3, and pip comes from sudo apt install python3-pip. Inside an activated venv both python and pip exist.
ModuleNotFoundError: No module named 'requests' almost always means the package was installed into a different Python from the one running your script: the venv isn't activated, the script runs under sudo or cron (both use the system Python), or your editor picked another interpreter. Ask Python which one it is:
$ python -c "import sys; print(sys.executable)"
$ python -m pip --version # shows which Python this pip installs into
Also check the import name, which isn't always the package name: pip install PyYAML gives import yaml, pip install python-dotenv gives import dotenv, pip install beautifulsoup4 gives import bs4.