Every CI job running tox, every pre-commit hook, every scratch install eventually reaches the same few thousand lines of Python that pypa/virtualenv keeps polishing. When it misbehaves, the symptoms are annoying: a fresh environment with no pip inside it, an activation script that mangles a path with spaces, a cold runner rebuilding what the previous job already built. Each failure mode has a dedicated guard in the source, from symlink detection to SHA-256 verification of bundled wheels.
The common assumption is that virtualenv wraps the stdlib venv module. The source says otherwise: it is a plugin registry driven by entry points, a per-user cache with reentrant file locks and format-versioned folders, and a wheel supply chain that verifies digests before anything touches your environment. The stdlib venv is one selectable creator among eleven, and not the default.
This post walks the current main branch. Every symbol and line number below was checked against the real tree, and both diagrams were generated from those same sources. If you have wondered what happens between typing virtualenv .venv and seeing the prompt change, the sections below follow that path.
The creation flow: one CLI entry fans out into config parsing, discovery, three plugin selectors, and the create-seed-activate sequence.
Reading the overview from left to right:
- Session.run, L81 — the whole job: create, seed, generate activators, write pyvenv.cfg.
- cli_run, L46 — the public API: build a session from arguments and run it in a with block.
- VirtualEnvConfigParser, L42 — an argparse subclass rewriting defaults from env vars and ini files before every parse.
- Builtin.run, L85 — interpreter discovery tries each -p spec in turn and returns the first match.
- CreatorSelector.for_interpreter, L45 — filters creators by can_create and reserves the first builtin as the default choice.
- Creator.run, L345 — the shared artifact writer: pyvenv.cfg, CACHEDIR.TAG, VCS ignore files and the .venv redirect.
- FromAppData.run, L103 — the seeder installs pip and setuptools from wheel images cached in the app-data folder.
- BUNDLE_SHA256, L73 — every shipped wheel carries a recorded digest verified on load.
- periodic_update, L83 — checks PyPI for newer seed wheels in a background process every fourteen days.
The detail view adds the plugin selectors, creator hierarchy, wheel resolution chain and each app-data store.
One command, five layers
The console script is wired to run_with_catch in main. cli_run delegates to session_via_cli, and that function stages everything. build_parser first runs a hidden argparse pass validating that early options like –python have values, since staged parsing can otherwise act on later flags before noticing an earlier one is missing. load_app_data wires the cache: –app-data is typed as a partial of make_app_data, so the store is built during parsing, and –reset-app-data wipes it immediately if requested. Discovery runs there too: get_discover reads the virtualenv.discovery group, the builtin resolves the interpreter, and None raises a RuntimeError naming the request. Only then are the three ComponentBuilders constructed: CreatorSelector, SeederSelector and ActivationSelector, each adding its own options and turning into a concrete object through e.create(options).
The Session object sequences the work: _create calls creator.run, _seed runs the seeder when enabled, _activate generates the selected scripts, and creator.pyenv_cfg.write persists the configuration. One subtlety: on Windows, if the batch activator supports the interpreter but was not selected, Session still calls its remove_unsafe to clean stale .bat files.
The config parser that remembers where values came from
virtualenv has three config sources: command-line flags, VIRTUALENV_-prefixed environment variables, and an ini file named by VIRTUALENV_CONFIG_FILE or found at virtualenv.ini in the user config directory; only its virtualenv section is read. The unusual part is bookkeeping: before each parse, VirtualEnvConfigParser._fix_default consults the environment and the ini file for every registered action, rewriting the argparse default to whatever it found. A sibling-flag rule lets an environment variable beat an ini value for –x/–no-x pairs, and a tri-state helper converts a configured false on a negative flag into “unset” instead of flipping the option on. ExtendAction keeps list-valued flags from mixing configured defaults with command-line values.
VirtualEnvOptions, the namespace subclass, records each value’s source, and the custom help formatter prints defaults as “from env var” or “from file”. The same tracking feeds behavior: ViaGlobalRefApi._should_symlink walks the priority levels cli, env var, file, default in order, and when –copies and –symlinks are set at the same level, copy wins.
Discovery: py_info left the building
The largest recent structural change is that interpreter introspection no longer lives in this repository. The pyproject declares a python-discovery dependency, and the old modules survive as shims: discovery/py_info.py re-exports PythonInfo, py_spec.py re-exports PythonSpec, cached_py_info.py forwards the cache helpers. Debugging why the wrong interpreter was picked now spans two packages.
What stays here is policy. The Builtin discovery takes specs from –python, defaulting to the running interpreter, and supports version specifiers such as >=3.12. The Discover base class caches the resolved interpreter lazily. Results are cached on disk under py_info/5, where each interpreter path gets a JSON file keyed by the SHA-256 hash of the path, so repeated creations skip re-probing. That number matters: it is how the cache invalidates when the stored PythonInfo shape changes.
Creators: builtin first, venv as a plugin
The virtualenv.create entry-point group registers eleven creators: four cpython3 variants including macOS Homebrew and framework builds, two each for GraalPy, PyPy and RustPython, plus one plain venv. The name builtin is reserved, and CreatorSelector hands that slot to the first VirtualenvBuiltin subclass it encounters — a class inheriting both Creator and Describe, so it builds an environment and can describe an existing one. Because the selector sorts builtin first, that classic implementation runs when you never pass –creator.
The builtin CPython creators are file movers with strong opinions. On POSIX, sources yields the executables and, when the interpreter reports Py_ENABLE_SHARED with INSTSONAME and LIBDIR, also the shared libpython library. On Windows it yields executables, DLL and pyd files, and the embedded python zip, skipping pywin32 DLLs to avoid clobbering that runtime. From Python 3.13 the creator prefers the venvlauncher shims CPython now ships, avoiding copies of python.exe entirely. Windows Store Python is rejected outright: setup_meta returns None for it, so it falls off the candidate list.
The venv creator delegates to the standard library instead. It runs venv.EnvBuilder with with_pip=False, either inline when the host interpreter is the current one, or through a subprocess passing –without-pip and, from 3.13 on, –without-scm-ignore-files. Before delegating it unlinks any symlink or hard-linked interpreter file in the destination, since stdlib venv writes files in place and a planted link would receive them outside the environment. On POSIX, a copy-mode probe creates a temporary file to learn what permissions the filesystem allows, then hides the bin directory until copied files are fixed up.
All of this sits on top of Creator.run, which handles the shared artifacts. validate_dest refuses an empty destination, any path containing os.pathsep, and a dash quirk where a percent sign in a PATH entry reads as an option delimiter. After creation you get pyvenv.cfg with home, implementation, version and command keys, a CACHEDIR.TAG with the standard cache signature, a one-line .gitignore ignoring everything, and — since this year — a PEP-832 style .venv redirect pointing at the environment, written by default only beside a pyproject.toml without an existing .venv. The class layout is easiest to follow in the CPython source this code mirrors.
From bundled wheels to a SHA-256-verified cache
Seeding changed the most: the embed folder of the seed package ships exactly four wheel files today: two pip releases, pip-26.0.1 and pip-26.3, and two setuptools releases, setuptools-82.0.1 for Python 3.9 and setuptools-84.2.1 for 3.10 through 3.16. The wheel distribution is gone: the old –wheel and –no-wheel flags parse but only emit a deprecation warning, with removal promised after 2026-12. From Python 3.12 on, setuptools is not seeded by default, since those interpreters no longer need it to build packages. Below 3.9 the seeder refuses outright, with cannot_seed naming Python 3.9 as the oldest bundled target.
Integrity moved the other way: stricter, not looser. BUNDLE_SHA256 records the digest of every bundled wheel, generated alongside BUNDLE_SUPPORT by the upgrade task, and get_embed_wheel verifies the file on first use, memoizing verified names; from a zipapp the hashing helper reads the bytes out of the archive. Downloaded wheels go through verify_wheel_digest, which asks the PyPI JSON API for the release’s recorded sha256, retries after one, two and four seconds, and refuses the wheel with UnverifiedWheelError if the digest can never be obtained — so blocking the PyPI API cannot silently disable the check.
The app-data cache makes repeat creation fast; its layout is documented as a literal directory tree at the top of via_disk_folder.py: py_info/5 for interpreter facts, wheel/house for downloaded wheels, wheel/
FromAppData turns that cache into installs. Worker threads resolve wheels through get_wheel, with download as a fallback, then each distribution is materialized as a wheel image exactly once under a non-reentrant lock, and installed by CopyPipInstall or SymlinkPipInstall — the latter only when symlinks are supported and the seed pip is at least 19.3. Failure handling is deliberate: worker exceptions are collected and raised together after every install finishes; a second Ctrl+C during seeding exits with code 130 instead of unwinding the executor. Update timing is equally explicit: UPDATE_PERIOD is fourteen days, a CI grace period of one hour prevents a scheduled run from adopting a wheel published mid-job, minor releases wait twenty-eight days, and the background updater runs as a subprocess with PYTHONSAFEPATH set and no console window on Windows.
Honest Limits
The store-python rejection is one edge: if no creator claims a Windows Store interpreter, discovery succeeds but creation fails with a runtime error listing the interpreter. Debugging discovery spans two repositories, since PythonInfo lives in python_discovery while its cache lives here, and the shims can mask which module runs. The shared cache is best-effort: UpdateLog drops malformed JSON with a warning, and a custom-index wheel whose directory no longer matches its digest is skipped rather than trusted. verify_wheel_digest fails loud on a PyPI outage by design, so an offline runner without a cached wheel cannot download fresh. Symlink seeding silently downgrades to copying without symlink support, and the PEP-832 .venv redirect is provisional, so consumers should not treat it as stable. None are bugs; they are the documented edges of a tool that prefers failing visibly over surprising quietly. Enjoyed this post? Never miss out on future posts by following us