Plugin API reference¶
This page documents the interfaces that plugins must implement.
Discovery¶
Discovery plugins locate Python interpreters for creating virtual environments.
- class virtualenv.discovery.discover.Discover(options)¶
Base for discovering a Python interpreter, which subclasses implement in
run.- Parameters:
options (
VirtualEnvOptions) – the parsed options, providing the environment to search
- property interpreter: PythonInfo | None¶
The interpreter as returned by
run(), cached.
- abstractmethod run()¶
Return the interpreter this discovery finds.
- Return type:
- Returns:
the interpreter,
Nonewhen none is found- Raises:
NotImplementedError – always, subclasses implement it
- classmethod add_parser_arguments(parser)¶
Add the command line options this discovery reads.
- Parameters:
parser (
ArgumentParser) – the parser to add the options to- Raises:
NotImplementedError – always, subclasses implement it
- Return type:
PythonInfo¶
Discovery plugins return a PythonInfo object describing the located interpreter.
- class virtualenv.discovery.py_info.PythonInfo¶
Contains information for a Python interpreter.
- Variables:
platform (
str) – the platform name, as reported bysys.platform.implementation (
str) – the implementation name, such asCPython.pypy_version_info (
tuple[int,...]) – the PyPy version tuple, empty for other implementations.version_info (
VersionInfo) – the interpreter version as a named tuple.architecture (
int) – the pointer width in bits,32or64.sysconfig_platform – the platform string from
sysconfig, orNone.version_nodot (
str|None) – themajor.minorversion without the dot.version (
str) – the full version string fromsys.version.os (
str) – the operating system name, as reported byos.name.free_threaded (
bool) – whether the interpreter was built free-threaded.debug_build (
bool) – whether the interpreter was built withPy_DEBUG.marker_environment (
MarkerEnvironment) – the PEP 508 marker environment of the interpreter.base_prefix (
str|None) – thesys.base_prefixof the interpreter.real_prefix (
str|None) – thesys.real_prefixset by legacy virtualenv, orNone.base_exec_prefix (
str|None) – thesys.base_exec_prefixof the interpreter.exec_prefix (
str|None) – thesys.exec_prefixof the interpreter.executable (
str) – thesys.executableof the interpreter.original_executable (
str) – the absolute path to the executable, without resolving symlinks.system_executable (
str|None) – the system interpreter behind any virtualenv, orNone.has_venv – whether the
venvmodule can be imported.file_system_encoding (
str) – the filesystem encoding, fromsys.getfilesystemencoding().stdout_encoding (
str|None) – the encoding ofsys.stdout, orNone.sysconfig_scheme (
str|None) – the installation scheme behindsysconfig_paths, orNone.sysconfig_paths – the installation paths, keyed by
sysconfigpath name.distutils_install (
dict[str,str]) – the distutils installation directories, empty when sysconfig supplies them.sysconfig – the
sysconfigbuild metadata, such as the makefile filename.sysconfig_vars – the
sysconfigconfig variables queried for this interpreter.system_stdlib (
str) – the standard library path of the system interpreter.system_stdlib_platform (
str) – the platform standard library path of the system interpreter.max_size (
int) – thesys.maxsizeof the interpreter.
- version_info: VersionInfo¶
- marker_environment: MarkerEnvironment¶
- classmethod from_dict(data)¶
Reconstruct a
PythonInfofrom a plain dictionary.
- classmethod current(cache=None)¶
Locate the current host interpreter information.
- Parameters:
cache (
PyInfoCache|None) – interpreter metadata cache; whenNoneresults are not cached.- Return type:
- Returns:
the information about the running interpreter.
- Raises:
RuntimeError – if the running interpreter cannot be queried.
- classmethod from_exe(exe, cache=None, *, raise_on_error=True, ignore_cache=False, resolve_to_host=True, env=None)¶
Get the python information for a given executable path.
- Parameters:
exe (
str) – path to the Python executable.cache (
PyInfoCache|None) – interpreter metadata cache; whenNoneresults are not cached.raise_on_error (
bool) – raise on failure instead of returningNone.ignore_cache (
bool) – bypass the cache and re-query the interpreter.resolve_to_host (
bool) – resolve through virtualenv layers to the system interpreter.env (
Mapping[str,str] |None) – environment mapping; defaults toos.environ.
- Return type:
- Returns:
the interpreter information, or
Nonewhen it cannot be queried.
- classmethod current_system(cache=None)¶
Locate the current system interpreter information, resolving through any virtualenv layers.
- Parameters:
cache (
PyInfoCache|None) – interpreter metadata cache; whenNoneresults are not cached.- Return type:
- Returns:
the system interpreter behind any virtualenv layers.
- Raises:
RuntimeError – if the system interpreter cannot be queried.
- classmethod from_json(payload)¶
Deserialize interpreter information from a JSON string.
- Parameters:
- Return type:
- Returns:
the interpreter information decoded from payload.
- classmethod resolve_to_system(cache, target, *, env=None)¶
Walk virtualenv/venv prefix chains to find the underlying system interpreter.
- Parameters:
cache (
PyInfoCache|None) – interpreter metadata cache; whenNoneresults are not cached.target (
PythonInfo) – the interpreter to resolve.env (
Mapping[str,str] |None) – environment mapping; defaults toos.environ.
- Return type:
- Returns:
the system interpreter that target resolves to.
- Raises:
RuntimeError – if the prefixes form a cycle.
- property spec: str¶
A specification string identifying this interpreter (e.g.
CPython3.13.2-64-arm64).
- property machine: str¶
The instruction set architecture (ISA) derived from
sysconfig.get_platform().
- property is_old_virtualenv: bool¶
Trueif this interpreter runs inside an old-style virtualenv (hasreal_prefix).
- sysconfig_path(key, config_var=None, sep='/')¶
Return the sysconfig install path for a scheme key, optionally substituting config variables.
- Parameters:
- Return type:
- Returns:
the path, or an empty string if key is unknown.
- Raises:
ValueError – if the sysconfig pattern contains format specifiers.
- install_path(key)¶
Return the relative installation path for a given installation scheme key.
- property system_exec_prefix: str¶
The exec prefix of the system Python this interpreter is based on.
- property system_exe: str¶
The executable of the system Python this interpreter is based on.
system_executablestaysNoneuntilresolve_to_system()walks the prefix chain, so reading it forces every caller to narrow a value discovery has already settled. The fallback here matches what resolution itself writes when a prefix links back to its own interpreter.
- classmethod clear_cache(cache)¶
Clear all cached interpreter information from cache.
- Parameters:
cache (
PyInfoCache) – the cache store to clear.- Return type:
- satisfies(spec, *, impl_must_match)¶
Check if a given specification can be satisfied by this python interpreter instance.
- to_json()¶
Serialize this interpreter information to a JSON string.
- Return type:
- Returns:
the JSON document.
- to_dict()¶
Convert this interpreter information to a plain dictionary.
- discover_exe(cache, prefix, *, exact=True, env=None)¶
Discover a matching Python executable under a given prefix directory.
- Parameters:
- Return type:
- Returns:
the matching interpreter information.
- Raises:
RuntimeError – if no matching interpreter is found.
App data¶
The application data interface used by plugins for caching.
- class virtualenv.app_data.base.AppData¶
Abstract storage interface for the virtualenv application.
- Variables:
- property transient: bool¶
Trueif this app data store is transient and does not persist across runs.
- for_wheel_index(env, *, cached_custom=False)¶
Select separate wheel archives and update records for the configured index.
- abstractmethod py_info(path)¶
Return a content store for cached interpreter information at the given path.
- Parameters:
path (
Path) – the interpreter executable path- Return type:
ContentStore- Returns:
a content store for the cached data
- abstractmethod embed_update_log(distribution, for_py_version)¶
Return a content store for the embed update log of a distribution.
- abstractmethod wheel_image(for_py_version, name)¶
Return the path to a cached wheel image.
- ensure_extracted(path, to_folder=None)¶
Ensure a path is available on disk, extracting from zipapp if needed.
- abstractmethod extract(path, to_folder)¶
Extract a path from the zipapp to a location on disk.
Creators¶
Creator plugins build the virtual environment directory structure and install the Python interpreter.
- class virtualenv.create.creator.CreatorMeta¶
Result of a creator capability check, carrying the reason it cannot create an environment in
error.- Variables:
error – the reason the creator cannot create the environment,
Nonewhen it can
- class virtualenv.create.creator.Creator(options, interpreter)¶
A class that given a python Interpreter creates a virtual environment.
- Variables:
dest – the directory the virtual environment is created in
clear –
Trueto delete the destination directory before creating the environmentno_vcs_ignore –
Trueto skip writing the VCS ignore file into the destinationvenv_redirect –
Trueto always write the.venvredirect file,Falseto never write it,Noneto write it only beside a project folder that has no.venvyetpyenv_cfg – the
pyvenv.cfgof the destinationapp_data – the application data folder
env – the environment variables the creation runs with
prompt – the prompt prefix,
Nonewhen none was given
Construct a new virtual environment creator.
- Parameters:
options (
VirtualEnvOptions) – the CLI option as parsed fromadd_parser_arguments()interpreter (
PythonInfo) – the interpreter to create virtual environment from
- classmethod can_create(interpreter)¶
Determine if we can create a virtual environment.
- Parameters:
interpreter (
PythonInfo) – the interpreter in question- Return type:
CreatorMeta|bool|None- Returns:
Noneif we can’t create, any other object otherwise that will be forwarded toadd_parser_arguments()
- classmethod add_parser_arguments(parser, interpreter, meta, app_data)¶
Add CLI arguments for the creator.
- Parameters:
parser (
ArgumentParser) – the CLI parserinterpreter (
PythonInfo) – the interpreter we’re asked to create virtual environment formeta (
CreatorMeta) – value as returned bycan_create()app_data (
AppData) – the application data folder
- Return type:
- add_cachedir_tag()¶
Generate a file indicating that this is not meant to be backed up.
- Return type:
Seeders¶
Seeder plugins install initial packages (like pip, setuptools, wheel) into the virtual environment.
- class virtualenv.seed.seeder.Seeder(options, enabled)¶
A seeder will install some seed packages into a virtual environment.
- Variables:
env – the environment variables the seeding runs with
Create.
- Parameters:
options (
VirtualEnvOptions) – the parsed options as defined withinadd_parser_arguments()enabled (
bool) – a flag weather the seeder is enabled or not
- classmethod cannot_seed(interpreter)¶
Explain why this seeder cannot install seed packages for the given interpreter.
- Parameters:
interpreter (
PythonInfo) – the interpreter the environment is based on- Return type:
- Returns:
Nonewhen the seeder supports the interpreter, otherwise a message describing why it cannot; selection rejects a seeder that returns a message and surfaces it to the user
- classmethod add_parser_arguments(parser, interpreter, app_data)¶
Add CLI arguments for this seed mechanisms.
- Parameters:
parser (
ArgumentParser) – the CLI parserinterpreter (
PythonInfo) – the interpreter this virtual environment is based ofapp_data (
AppData) – the application data folder
- Return type:
- abstractmethod run(creator)¶
Perform the seed operation.
- Parameters:
creator (
Creator) – the creator (based ofvirtualenv.create.creator.Creator) we used to create this virtual environment- Return type:
Activators¶
Activator plugins generate shell-specific activation scripts.
- class virtualenv.activation.activator.Activator(options)¶
Generates activate script for the virtual environment.
- Variables:
flag_prompt – the prompt prefix shown while the environment is active
Create a new activator generator.
- Parameters:
options (
VirtualEnvOptions) – the parsed options as defined withinadd_parser_arguments()
- classmethod supports(interpreter)¶
Check if the activation script is supported in the given interpreter.
- Parameters:
interpreter (
PythonInfo) – the interpreter we need to support- Return type:
- Returns:
Trueif supported,Falseotherwise
- classmethod add_parser_arguments(parser, interpreter)¶
Add CLI arguments for this activation script.
- Parameters:
parser (
ArgumentParser) – the CLI parserinterpreter (
PythonInfo) – the interpreter this virtual environment is based of
- Return type:
- abstractmethod generate(creator)¶
Generate activate script for the given creator.
- Parameters:
creator (
Creator) – the creator (based ofvirtualenv.create.creator.Creator) we used to create this virtual environment- Return type:
- Returns:
the paths of the generated activation scripts