This article compares the three common ways to give a Python project its own isolated set of packages, for anyone setting up a new service or inheriting an old one. The names blur together, and every tutorial you read seems to recommend a different one. The short answer is to create the environment with venv, the module built into Python since 3.3, and to pin dependencies with a separate lockfile tool such as pip-tools, Poetry, or uv. A lockfile records the exact version of every package so the install repeats the same way on the next machine. Keep virtualenv for a few narrow cases, skip pipenv on new projects, and you will finish with a setup a teammate can reproduce.
How to verify
Confirm what you have and which Python the env points at:
python3 -m venv --help 2>/dev/null | head -2
virtualenv --version 2>/dev/null
pipenv --version 2>/dev/null
ls /opt/venvs/ 2>/dev/null
/opt/venvs/app/bin/python -c "import sys; print(sys.prefix, sys.base_prefix)"
If sys.prefix == sys.base_prefix, the shell is not in a venv — which python is the system one.
What’s happening
A Python “virtual environment” is a directory with a pyvenv.cfg, a bin/python symlink (or copy) pointing at a real interpreter, and an empty site-packages/. When the interpreter starts and sees pyvenv.cfg, it sets sys.prefix to that directory, so import looks at the venv’s site-packages instead of the system’s. That’s the entire trick. Both venv and virtualenv produce this shape; the differences are speed and scope.
venv is built into Python — python3 -m venv myenv works on any 3.3+ install with no extra packages. It’s slightly slower at creation (it installs pip by extracting an embedded wheel) and uses a copy of the interpreter on Windows. virtualenv is a PyPI package that pre-dates venv; it’s faster (clever caching of pip wheels) and supports Python 2 (irrelevant now). For Python 3, venv is enough.
pipenv is different in kind. It manages two things: the venv (using virtualenv under the hood) and dependencies (via Pipfile and Pipfile.lock). The lockfile concept is good; the implementation has been slow, the resolver was historically buggy, and the project’s release cadence has been erratic. Modern equivalents (Poetry, PDM, uv, pip-tools) do the same job better.
Decision
-
Use
venvfor the environment. It’s stdlib, has no install step, and produces a standard structure:python3 -m venv /opt/venvs/app /opt/venvs/app/bin/pip install --upgrade pipPin the venv path outside the project (e.g.,
/opt/venvs/<project>) so agit clean -fdxdoesn’t blow it away. -
Pair
venvwith pip-tools, Poetry, PDM, or uv for dependencies. The venv only isolates; it doesn’t pin. For a hash-pinned reproducible install:/opt/venvs/app/bin/pip install pip-tools /opt/venvs/app/bin/pip-compile --generate-hashes requirements.in /opt/venvs/app/bin/pip-sync requirements.txt -
Use
virtualenvonly if you specifically need: older Python compatibility (rare), faster env creation in CI loops where you spin up many envs per minute (uv is faster than both), or one of its plugins (none widely used today). -
Avoid
pipenvfor new projects. It still works, but the ecosystem has moved past it. If you inherit aPipfile, the migration to either pip-tools + venv or Poetry is straightforward:pipenv requirements > requirements.in # extract the deps pipenv requirements --dev >> requirements-dev.in # then compile with pip-tools -
In a Docker image, skip the venv entirely. The container is already isolated. Install directly into the system Python and shave a layer:
FROM python:3.12-slim COPY requirements.txt /tmp/ RUN pip install --no-cache-dir -r /tmp/requirements.txtThe exception is multi-stage builds where you want to copy the venv from a builder stage — then a
venvkeeps the boundary clean. -
Activate or not activate. Activation just prepends
bin/toPATH. For scripts and systemd units, call the venv’s Python by full path —/opt/venvs/app/bin/python— and skip activation entirely. Activation is for interactive shells.
Common pitfalls
- Committing the venv to git —
.gitignoreit. The venv is a build artifact; the recipe (requirements.txt) is the artifact. - Creating a venv inside the source tree and hardcoding it in systemd — a developer’s
python3 -m venv .venvcollides with the prod path. Prefix prod venvs with/opt/venvs/to avoid this. python3 -m venv --system-site-packages— inherits packages from the system, which defeats the point of isolation. Use only when you specifically need a system-installed extension (e.g.,apt-shipped GTK bindings).- Activating a venv in a subshell and assuming the parent shell inherits it — it doesn’t.
source venv/bin/activatemust run in the shell you’ll use. - Mixing the venv’s
pipwith a globally-installedpip— the global one writes to systemsite-packages, not the venv. Always use the venv’s full-path pip.
For the dependency-pinning side of the same setup, see Pinning Python dependencies with pip-tools and hashes. Stack Harbor’s managed operations practice standardises on venv at /opt/venvs/<project> paired with pip-tools or uv — pipenv is migrated out wherever it’s found, because the maintenance overhead has consistently exceeded the value.