Sub topic 14
Where virtualenv, pipx, and uv fit
This chapter places three tools next to venv and pip. It explains what each one adds and when to switch. It assumes you can create and activate a venv environment, install packages with python -m pip, and read a requirements.txt file. You should also know that an environment is a directory holding its own interpreter link and site-packages.
All three tools build on the same ideas you already know: an isolated directory, an interpreter that points into it, and packages installed from an index such as PyPI. None of them replaces the concept of a virtual environment. They differ in speed, in features, and in which problem they target.
The baseline: venv and pip
venv ships with Python, and pip is installed into each new environment by default. This pair has no extra install step and is available on any machine with Python 3. Tutorials, CI images, and documentation can assume it.
Its limits explain why the other tools exist:
venvcreates environments for the interpreter that runs it. To get another Python version, you must already have that interpreter installed and call it directly.pipinstalls what you ask for and resolves dependencies, but it has no lock file of its own. Reproducibility comes frompip freezeand constraints files that you maintain.- Creating an environment and installing into it can feel slow on large dependency sets.
- Installing command-line applications into a shared environment can cause dependency conflicts.
virtualenv
virtualenv is a third-party package that predates venv. The standard-library module was modeled on it. Today the two overlap heavily, and virtualenv remains useful in a few cases.
python -m pip install virtualenv
python -m virtualenv .venv
source .venv/bin/activate
python -c "import sys; print(sys.prefix)"
deactivateThe result has the same layout as a venv environment: a pyvenv.cfg file, an interpreter entry, and a site-packages directory. Activation scripts work the same way.
What virtualenv adds:
- Faster creation. It keeps a cache of the seed packages (pip and similar) and reuses them, rather than bootstrapping pip each time.
- Interpreter discovery. You can request a version with
-p, for examplevirtualenv -p python3.11 .venv. It searches the system for a matching interpreter. - Wider compatibility. It can create environments for some interpreters and Python versions that
venvcannot. - Configuration. Options can come from environment variables or a config file, which helps on teams that want the same defaults everywhere.
If the only thing you need is a project environment on a current Python, venv is enough. Reach for virtualenv when environment creation is a bottleneck, as in a test matrix that builds many environments. tox is one tool that does this, and it uses virtualenv for that reason.
pipx
pipx solves a different problem. Some Python packages are applications rather than libraries. Examples are black, ruff, httpie, and cookiecutter. You want their command on your PATH, but you do not want them in a project environment or in your system Python.
pipx installs each application into its own dedicated virtual environment and links only the application's commands into a directory on your PATH.
python -m pip install --user pipx
python -m pipx ensurepath
pipx install black
black --version
pipx list
pipx upgrade black
pipx uninstall blackpipx ensurepath adds the directory holding the linked commands to your shell's PATH. You usually need to open a new shell afterward. Many operating systems also package pipx themselves, and you can install it that way instead.
You can also run an application once without keeping it installed:
pipx run cowsay -t "hello"pipx run builds a temporary environment, runs the command, and caches the environment for later reuse.
When pipx is the wrong tool
pipx is for applications that you run, not libraries that you import. If you run pipx install requests, nothing useful happens, because requests provides no command. Your project code still needs requests in the project's own environment.
Sometimes a tool needs a plugin in the same environment. pipx inject adds a package to an existing pipx environment:
pipx inject black "black[d]"Use this only for plugins that the application loads. If your project's tests need a linter importable from the project's environment, install it there instead.
uv
uv is a package and project manager from Astral, written in Rust and distributed as a single binary. It is not part of Python and is not installed with it. It aims to cover what pip, venv, virtualenv, pipx, and some other tools do under one command. It is much faster than pip in most cases, mainly because of parallel downloads, a global cache, and a fast resolver.
uv can be used at two levels. The first is a drop-in layer that mirrors the commands you already know.
uv venv
source .venv/bin/activate
uv pip install requests
uv pip install -r requirements.txt
uv pip list
uv pip freezeuv venv creates .venv in the current directory by default. It does not install pip into the environment unless you pass --seed. This causes a common surprise: python -m pip install requests fails inside a uv environment with "No module named pip". The fix is to use uv pip install or to create the environment with uv venv --seed.
uv pip compile turns loose requirements into a fully pinned file, which fills the gap that pip leaves around lock files:
printf 'requests>=2.31,<3\n' > requirements.in
uv pip compile requirements.in -o requirements.txt
uv pip sync requirements.txtuv pip sync makes the environment match the file exactly, including removing packages that the file does not list. By contrast, pip install -r only adds or updates.
The second level is project management based on pyproject.toml:
uv init demo
cd demo
uv add requests
uv run python -c "import requests; print(requests.__version__)"
uv syncHere uv add records the dependency in pyproject.toml and updates a cross-platform lock file named uv.lock. It creates and updates the project's .venv automatically. uv run executes a command inside that environment, so you do not need to activate it. uv sync rebuilds the environment from the lock file.
uv also covers neighboring tasks:
uv tool install blackanduvx blackplay the role ofpipx installandpipx run.uv python install 3.12downloads and manages Python interpreters, so a missing version no longer blocks you.
Trade-offs of uv
- Extra dependency. Every developer and CI job needs
uvinstalled. Standard-libraryvenvneeds nothing. - Different file formats.
uv.lockis specific touv. Arequirements.txtproduced byuv pip compileis still readable by plainpip, which makes it the safer interchange format. - Not identical to pip. The
uv pipinterface covers the common commands, but not every option and edge case. Resolution can differ frompipin rare cases. Check the documentation for any flag you depend on. - Fast-moving. Features and defaults change faster than those of the standard library. Pin the
uvversion in CI if you rely on specific behavior.
Comparing the tools
| Tool | Part of Python | Main job | Typical command |
|---|---|---|---|
| `venv` | Yes | Create a project environment | `python -m venv .venv` |
| `pip` | Bundled in new environments | Install and remove packages | `python -m pip install requests` |
| `virtualenv` | No | Faster, more flexible environment creation | `virtualenv -p python3.11 .venv` |
| `pipx` | No | Install command-line applications in isolation | `pipx install black` |
| `uv` | No | Environments, installs, locking, tools, and Python versions | `uv add requests` |
Choosing between them
Some practical rules:
- Use
venvandpipwhen you write documentation, teach, or support machines you do not control. - Use
pipxoruv toolfor developer utilities, so they never collide with project dependencies. - Use
uvwhen install time, lock files, or Python version management matter to your team. - Use
virtualenvwhen a tool or workflow already depends on it, or when you need its interpreter discovery. - Keep a
requirements.txtorpyproject.tomlthat plainpipcan read if others on the project do not useuv.
Exercise
You created an environment with uv venv. Running python -m pip install requests inside it fails with "No module named pip". Why does this happen, and what are two ways to fix it?
Show answer
uv venv does not install pip into the environment by default, so the pip module does not exist there. One fix is to install with uv pip install requests. The other is to recreate the environment with uv venv --seed, which installs pip, after which python -m pip works.
Exercise
You want the black formatter available in every terminal, but you do not want it to appear in any project's pip list. Which tool fits, and what does it do to keep black separate?
Show answer
Use pipx install black (or uv tool install black). It installs black into its own dedicated virtual environment and links only the black command onto your PATH. Project environments never see the package, so it does not show up in their pip list.