Sub topic 3
Inside a virtual environment: pyvenv.cfg, the interpreter, and site-packages
This chapter assumes you can run python -m venv .venv from the previous chapter and are comfortable in a terminal. Here you will look at what that command created and how Python uses it.
The layout on disk
A virtual environment is an ordinary directory. On Linux and macOS it looks like this:
.venv/
├── bin/ # python, pip, activate scripts
├── include/
├── lib/
│ └── python3.12/
│ └── site-packages/
└── pyvenv.cfgOn Windows the names differ. Scripts\ replaces bin/, and Lib\site-packages\ replaces lib/python3.12/site-packages/.
| Part | Purpose |
|---|---|
| `pyvenv.cfg` | Marks the directory as an environment and points to the base installation |
| `bin/python` (`Scripts\python.exe`) | The interpreter that runs inside the environment |
| `site-packages` | Where `pip` puts third-party packages for this environment |
| `bin/pip`, `activate` | Helper executables and shell scripts |
pyvenv.cfg
This small text file is the key to the whole mechanism. Print it:
cat .venv/pyvenv.cfgA typical result:
home = /usr/bin
include-system-site-packages = false
version = 3.12.3home is the directory of the base interpreter that created the environment. include-system-site-packages = false means the environment cannot see packages installed in the base installation. Recent Python versions may add more keys, such as executable and command.
The interpreter
On Linux and macOS, .venv/bin/python is normally a symbolic link to the base interpreter. It is not a full copy. On Windows, Scripts\python.exe is a small launcher program.
When the interpreter starts, it looks for pyvenv.cfg next to the executable, or one directory above it. If it finds the file, it treats the directory as an environment. It sets sys.prefix to the environment directory and sys.base_prefix to the base installation. This is why running .venv/bin/python works even if you never activate anything.
Checking from Python
You can confirm which environment is running from inside Python. Save this as where.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())Run it with the environment's interpreter directly:
.venv/bin/python where.pyInside an environment, sys.prefix and sys.base_prefix differ, and site-packages points inside .venv. Run the same script with your system Python and the two prefixes will match.
site-packages
When you run python -m pip install requests, pip writes the package into the environment's site-packages directory. You will see a requests folder and a requests-<version>.dist-info folder holding metadata. import requests works because the site module puts that directory on sys.path at startup.
ls .venv/lib/python3.12/site-packagesA new environment contains only pip (and on some Python versions setuptools). Nothing from the base installation's site-packages is visible, unless you created the environment with --system-site-packages.
Exercise
You run .venv/bin/python -c "import sys; print(sys.prefix == sys.base_prefix)" without activating the environment. What does it print, and why?
Show answer
It prints False. The interpreter finds pyvenv.cfg beside the executable's directory, so it sets sys.prefix to the environment while sys.base_prefix stays at the base installation. Activation is not needed for this detection.