Inside a Virtual Environment
A virtual environment ("venv") is a directory that makes a Python interpreter behave as if it had its own private site-packages. It is not a container, not a copy of Python and not magic: it's a few symlinks, a config file and an empty package folder. Once you've looked inside one, a lot of packaging behaviour stops being mysterious.
Why environments exist
One Python installation has one site-packages. If project A needs requests 2.31 and project B needs 2.34, they can't both be satisfied there. Worse, on Linux and macOS the system Python belongs to the OS, and its tools depend on the packages installed in it, so pip install into it can break the OS. Since PEP 668, distributions mark their Python as externally managed and pip refuses:
pip install requests 2>&1
echo "exit code: $?"error: externally-managed-environment
× This environment is externally managed
╰─> This Python is managed by the system image. To install Python packages, create a virtual environment with "python3 -m venv .venv" (or "uv venv") and use its pip, or use "uv run" / "poetry install".
note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
exit code: 1
(The wording of the message comes from the distribution; this container uses its own text. On Debian or Ubuntu it suggests apt install python3-xyz or pipx.) The fix is never --break-system-packages; it is a virtual environment.
What python -m venv creates
python3 -m venv .venv
tree -L 2 --noreport .venv | grep -v Activate.ps1
echo "--- pyvenv.cfg"
cat .venv/pyvenv.cfg
echo "--- the interpreter is a symlink"
readlink .venv/bin/python .venv/bin/python3
echo "--- site-packages holds only pip"
ls .venv/lib/python3.13/site-packages.venv
├── bin
│ ├── activate
│ ├── activate.csh
│ ├── activate.fish
│ ├── pip
│ ├── pip3
│ ├── pip3.13
│ ├── python -> python3
│ ├── python3 -> /usr/local/bin/python3
│ └── python3.13 -> python3
├── include
│ └── python3.13
├── lib
│ └── python3.13
├── lib64 -> lib
└── pyvenv.cfg
--- pyvenv.cfg
home = /usr/local/bin
include-system-site-packages = false
version = 3.13.17
executable = /opt/python/bin/python3.13
command = /usr/local/bin/python3 -m venv /home/learner/.venv
--- the interpreter is a symlink
python3
/usr/local/bin/python3
--- site-packages holds only pip
pip
pip-26.2.2.dist-info
The pieces:
| Path | Purpose |
|---|---|
bin/python, python3, python3.13 |
symlinks to the base interpreter (on Windows: Scripts\python.exe, a small launcher) |
pyvenv.cfg |
the marker file. home says where the base Python lives; include-system-site-packages = false hides the base's packages |
lib/python3.13/site-packages |
the environment's private packages; lib64 is a symlink to lib on some Linux systems |
bin/pip, bin/activate* |
pip (seeded with ensurepip) and shell scripts that put bin/ first on PATH |
How Python notices it's in a venv
When Python starts, it looks for pyvenv.cfg next to the executable or one directory up. If it finds one, it sets sys.prefix to the environment's directory (and keeps the real installation in sys.base_prefix), and builds sys.path from the environment's site-packages. No environment variables are involved, so running .venv/bin/python directly is exactly the same as activating first:
python3 -m venv .venv
.venv/bin/python - <<'PY'
import sys
print("prefix: ", sys.prefix)
print("base_prefix:", sys.base_prefix)
print("in a venv: ", sys.prefix != sys.base_prefix)
print("site dir: ", [p for p in sys.path if p.endswith("site-packages")])
PY
echo "--- the system interpreter"
python3 -c 'import sys; print(sys.prefix == sys.base_prefix)'prefix: /home/learner/.venv
base_prefix: /opt/python
in a venv: True
site dir: ['/home/learner/.venv/lib/python3.13/site-packages']
--- the system interpreter
True
What activation does (and doesn't)
source .venv/bin/activate only changes your shell: it prepends .venv/bin to PATH, sets VIRTUAL_ENV, and changes the prompt. After that, python and pip find the environment's copies first. deactivate undoes it.
python3 -m venv .venv
echo "before: $(command -v python)"
. .venv/bin/activate
echo "after: $(command -v python) VIRTUAL_ENV=$VIRTUAL_ENV"
pip install -q attrs==26.1.0
deactivate
echo "after deactivate: $(command -v python)"
.venv/bin/python -c 'import attrs; print("attrs", attrs.__version__, "still usable without activating")'before: /usr/local/bin/python
after: /home/learner/.venv/bin/python VIRTUAL_ENV=/home/learner/.venv
after deactivate: /usr/local/bin/python
attrs 26.1.0 still usable without activating
That's why scripts, cron jobs, systemd units and Dockerfiles should call .venv/bin/python (or .venv/bin/<tool>) directly instead of activating: there's no shell state to get wrong.
Rules that follow from this design
- A venv is not relocatable. Scripts in
bin/have the absolute interpreter path in their shebang (#!/home/learner/.venv/bin/python3). Move or rename the directory and they break: recreate it instead. - A venv is tied to its base Python. Upgrade or delete the base interpreter (e.g. a Homebrew Python update) and the symlinks dangle. Again: recreate.
- Don't commit it. Add
.venv/to.gitignore(uv and Poetry do it for you) and commit the lockfile instead. - Name it
.venvin the project root. Editors (VS Code, PyCharm), uv and Poetry (within-projectvenvs) find it automatically.
A runnable model: finding the environment
This model mimics the start-up rule: look for pyvenv.cfg beside the executable or one directory up, and derive the prefix and site-packages from it:
files = {
"/home/ada/proj/.venv/pyvenv.cfg": "home = /usr/bin\ninclude-system-site-packages = false\nversion = 3.13.17",
}
def startup(executable):
bindir = executable.rsplit("/", 1)[0]
for candidate in (bindir, bindir.rsplit("/", 1)[0]):
cfg = files.get(candidate + "/pyvenv.cfg")
if cfg:
conf = dict(line.split(" = ") for line in cfg.splitlines())
paths = [candidate + "/lib/python3.13/site-packages"]
if conf["include-system-site-packages"] == "true":
paths.append("/usr/lib/python3.13/site-packages")
return f"venv {candidate} (base in {conf['home']}) -> {paths}"
return "no venv -> ['/usr/lib/python3.13/site-packages']"
print(startup("/home/ada/proj/.venv/bin/python"))
print(startup("/usr/bin/python3"))
files["/home/ada/proj/.venv/pyvenv.cfg"] = files["/home/ada/proj/.venv/pyvenv.cfg"].replace("false", "true")
print(startup("/home/ada/proj/.venv/bin/python"))venv /home/ada/proj/.venv (base in /usr/bin) -> ['/home/ada/proj/.venv/lib/python3.13/site-packages']
no venv -> ['/usr/lib/python3.13/site-packages']
venv /home/ada/proj/.venv (base in /usr/bin) -> ['/home/ada/proj/.venv/lib/python3.13/site-packages', '/usr/lib/python3.13/site-packages']
The last line is python -m venv --system-site-packages: the environment's packages come first, but the base installation's are visible too. It's occasionally useful (huge system-provided libraries) and usually a source of confusion.
Test yourself
Is .venv/bin/python script.py equivalent to activating the venv and running python script.py?
- No, activation sets up sys.path
- Yes: Python detects the venv from pyvenv.cfg next to its executable; activation only changes the shell's PATH
- Only on Windows
- Only if VIRTUAL_ENV is set
What does include-system-site-packages = false in pyvenv.cfg do?
- Prevents pip from installing
- Hides the base installation's site-packages from the environment
- Disables the standard library
- Makes the venv read-only
You renamed your project folder and .venv/bin/pytest now fails with 'bad interpreter'. Why?
- pytest is corrupt
- Scripts in a venv have the absolute interpreter path in their shebang, so venvs aren't relocatable
- The rename deleted site-packages
- pyvenv.cfg is missing