Sub topic 3
Inside a virtual environment: pyvenv.cfg, the interpreter, and site-packages
This chapter assumes you can run python -m venv .venv and activate the result. It also assumes you know what import does and what sys.path is. Here you will look at what venv actually creates and how the interpreter uses it. That explains why activation is optional, why environments can't be moved, and why pip installs land where they do.
What `venv` creates
A virtual environment is an ordinary directory. It holds a small configuration file, a pointer to a Python interpreter, some scripts, and an empty site-packages directory. It does not hold a copy of the standard library. That stays in the base installation.
On Linux and macOS, python3 -m venv .venv produces roughly this:
.venv/
├── bin/
│ ├── activate
│ ├── pip
│ ├── pip3
│ ├── python -> /usr/bin/python3
│ └── python3 -> python
├── include/
├── lib/
│ └── python3.12/
│ └── site-packages/
├── lib64 -> lib
└── pyvenv.cfgThe exact names vary with your Python version and distribution. lib64 is a symlink to lib that appears on some 64-bit Linux builds. On Windows the layout differs in naming only:
| Purpose | Linux and macOS | Windows |
|---|---|---|
| Interpreter and scripts | `bin/` | `Scripts/` |
| Installed packages | `lib/python3.X/site-packages/` | `Lib/site-packages/` |
| C headers | `include/` | `Include/` |
| Configuration | `pyvenv.cfg` | `pyvenv.cfg` |
On Windows, Scripts/python.exe is a small launcher stub, not a symlink, because symlinks need extra privileges there. You can also ask for copies on other platforms with python3 -m venv --copies .venv.
The pyvenv.cfg file
pyvenv.cfg is a plain text file of key = value lines. It is what makes the directory a virtual environment, as far as the interpreter is concerned.
home = /usr/bin
include-system-site-packages = false
version = 3.12.3
executable = /usr/bin/python3.12
command = /usr/bin/python3 -m venv /home/ana/project/.venv| Key | Meaning |
|---|---|
| `home` | Directory of the base interpreter that created the environment. Python uses it to find the standard library. |
| `include-system-site-packages` | Whether the base installation's `site-packages` stays visible. Default is `false`. |
| `version` | Version of the interpreter that created the environment. |
| `executable`, `command` | Recorded by newer Python versions (3.11 and later) for diagnostics. They are informational. |
The most important line is home. If the base interpreter is removed or upgraded to a path that no longer matches, home points at nothing. The environment then fails to start. The troubleshooting chapter returns to this.
How the interpreter finds its environment
There is no magic variable and no special mode. When you run .venv/bin/python, the interpreter does this at startup:
- It looks at the path it was launched with. It does not resolve symlinks first, so it sees
.venv/bin/python, not/usr/bin/python3. - It looks for
pyvenv.cfgin that executable's directory, then in the parent directory. - If it finds one, it sets
sys.prefixto the environment directory. It setssys.base_prefixto the installation named byhome. - The
sitemodule adds the environment'ssite-packagestosys.path.
Step 1 matters. Because the symlink is not resolved, the same binary behaves differently depending on which path you call it by. /usr/bin/python3 runs the base installation. .venv/bin/python runs the same binary but finds pyvenv.cfg and becomes the environment.
Seeing it from Python
The two prefixes are the reliable way to detect an environment. Save this as inspect_env.py:
import site
import sys
print("executable: ", sys.executable)
print("prefix: ", sys.prefix)
print("base_prefix: ", sys.base_prefix)
print("in a venv: ", sys.prefix != sys.base_prefix)
print("site-packages: ", site.getsitepackages())
print("sys.path entries:")
for entry in sys.path:
print(" ", entry)Run it with the base interpreter and then with the environment's interpreter:
python3 inspect_env.py
.venv/bin/python inspect_env.pyWith the base interpreter, prefix and base_prefix are equal and in a venv is False. With the environment's interpreter, prefix ends in .venv, base_prefix is the base installation such as /usr, and in a venv is True. The sys.path list now contains the environment's site-packages and still contains the base standard library directories. The standard library is shared. Third-party packages are not.
site-packages
site-packages is the directory where pip puts installed distributions. For each package you will find the importable module or package directory. You will also find a *.dist-info directory holding metadata such as the version, the list of installed files, and the dependency declarations. Commands like pip list and pip show read those dist-info directories. They do not consult a central database.
Install something small and look:
.venv/bin/python -m pip install requests
ls .venv/lib/python3.12/site-packagesYou should see requests, requests-<version>.dist-info, and its dependencies such as urllib3 and certifi. Because the directory belongs to the environment, deleting .venv removes every package you installed there and touches nothing else on the system.
Sharing the base site-packages
By default the environment cannot see packages installed in the base installation. You can change this at creation time:
python3 -m venv --system-site-packages .venv-sharedThis sets include-system-site-packages = true in pyvenv.cfg. The base site-packages is then added to sys.path after the environment's own, so the environment's packages win on conflicts. This is occasionally useful for large packages that are hard to build, or that your distribution installs for you. It also weakens isolation, because the environment's behavior now depends on whatever the system has installed. Leave it off unless you have a specific reason.
The scripts directory
bin/ (or Scripts/ on Windows) holds three kinds of files:
- The interpreter.
pythonandpython3, as symlinks or launcher stubs. - Activation scripts.
activatefor POSIX shells,activate.fish,activate.csh, andActivate.ps1andactivate.baton Windows. - Entry-point scripts. These are generated by
pipfor installed tools, such aspipitself orpytest.
Activation only changes your shell. It puts the scripts directory at the front of PATH, sets VIRTUAL_ENV, and usually changes the prompt. Nothing in the interpreter depends on it. This is why both of these work without activating:
.venv/bin/python -m pip list
.venv/bin/pytestThis form is common in cron jobs, systemd units, and CI scripts. Naming the interpreter explicitly removes any doubt about which environment you are using.
Entry-point scripts show the limits of the design. Open one:
head -n 1 .venv/bin/pipThe first line is a shebang that contains the absolute path to the environment's interpreter, for example #!/home/ana/project/.venv/bin/python. The path is written when the script is generated.
Common misreadings
- "The environment contains Python." It contains a pointer to Python and a private package directory. Upgrading or deleting the base interpreter can break it.
- "Activation is what isolates." Isolation comes from which interpreter you run. Activation just makes the right one the default on
PATH. - "
pipis a global tool." Thepipinbin/belongs to the environment. A differentpipearlier onPATHbelongs to something else. Usingpython -m pipavoids the question, because it uses whichever interpreter you named. - "
sys.prefixalways names the Python installation." Inside an environment it names the environment. Usesys.base_prefixfor the installation.
Exercise
You run .venv/bin/python -c "import sys; print(sys.prefix == sys.base_prefix)" in a normal environment created with python3 -m venv .venv, without activating it. What does it print, and which file makes the interpreter behave that way?
Show answer
It prints False. The interpreter finds pyvenv.cfg in the parent of its own bin directory, sets sys.prefix to the environment directory, and takes sys.base_prefix from the home entry. Activation is not needed, because the result depends on the path the interpreter was launched with.
Exercise
A teammate zips their .venv directory and sends it to you on another machine. Running .venv/bin/pip fails with "bad interpreter". Why, and what should they have sent instead?
Show answer
Entry-point scripts such as pip begin with a shebang holding the absolute path of the interpreter on the original machine, and pyvenv.cfg records the original home path. Neither is valid on your machine. They should send the project's requirements file and let you create a fresh environment with python -m venv and python -m pip install -r requirements.txt.