prefix_data#

Tools for managing the packages installed within an environment.

Classes#

PrefixDataType

Basic caching of PrefixData instance objects.

PrefixRecordDict

Lazily convert dict entries to PrefixRecord.

PrefixData

The PrefixData class aims to be the representation of the state

Functions#

get_conda_anchor_files_and_records(...)

Return the anchor files for the conda records of python packages.

delete_prefix_from_linked_data(→ bool)

Here, path may be a complete prefix or a dist inside a prefix

Attributes#

T

T#
class PrefixDataType#

Bases: type

Basic caching of PrefixData instance objects.

__call__(prefix_path: conda.common.path.PathType, interoperability: bool | None = None) PrefixData#
class PrefixRecordDict(dict=None, /, **kwargs)#

Bases: collections.UserDict

Lazily convert dict entries to PrefixRecord.

__getitem__(package_name: str) conda.models.records.PrefixRecord#
class PrefixData(prefix_path: conda.common.path.PathType, interoperability: bool | None = None)#

The PrefixData class aims to be the representation of the state of a conda environment on disk. The directory where the environment lives is called prefix.

This class supports different types of tasks:

  • Reading and querying conda-meta/*.json files as PrefixRecord objects

  • Reading and writing environment-specific configuration (env vars, state file, nonadmin markers, etc)

  • Existence checks and validations of name, path, and magic files / markers

  • Exposing non-conda packages installed in prefix as PrefixRecord, via the plugin system

_cache_: dict[tuple[pathlib.Path, bool | None], PrefixData]#
prefix_path: pathlib.Path#
_magic_file: pathlib.Path#
_frozen_file: pathlib.Path#
__prefix_records: dict[str, conda.models.records.PrefixRecord] | None = None#
__is_writable: bool | None | conda.auxlib._Null#
interoperability: bool = None#
classmethod from_name(name: str, **kwargs) PrefixData#

Creates a PrefixData instance from an environment name.

The name will be validated with PrefixData.validate_name() if it does not exist.

Parameters:

name -- The name of the environment. Must not contain path separators (/, ).

Raises:

CondaValueError -- If name contains a path separator.

classmethod from_context(validate: bool = False, **kwargs) PrefixData#

Creates a PrefixData instance from the path specified by context.target_prefix.

The path and name will be validated with PrefixData.validate_path() and PrefixData.validate_name(), respectively, if validate is True.

Parameters:
  • validate -- Whether the path and name should be validated. Useful for environments about to be created.

  • kwargs -- Additional keyword arguments to pass to the constructor.

property name: str#

Returns the name of the environment, if available.

If the environment doesn't live in one the configured envs_dirs, an empty string is returned. The construct prefix_data.name or prefix_data.prefix_path can be helpful in those cases.

__eq__(other: Any) bool#
exists() bool#

Check whether the PrefixData path exists and is a directory.

is_environment() bool#

Check whether the PrefixData path is a valid conda environment.

This is assessed by checking if conda-meta/history marker file exists.

is_frozen() bool#

Check whether the environment is marked as frozen, as per CEP 22.

This is assessed by checking if conda-meta/frozen marker file exists.

is_base() bool#

Check whether the configured path refers to the base environment.

property is_writable: bool | None | conda.auxlib._Null#

Check whether the configured path is writable. This is assessed by checking whether conda-meta/history is writable. It if is, it is assumed that the rest of the directory tree is writable too.

Note: The value is cached in the instance. Use .assert_writable() for a non- cached check.

assert_exists() None#

Check whether the environment path exists.

Raises:

EnvironmentLocationNotFound -- If the check returns False.

assert_environment() None#

Check whether the environment path exists and is a valid conda environment.

Raises:

DirectoryNotACondaEnvironmentError -- If the check returns False.

assert_writable() None#

Check whether the environment path is a valid conda environment and is writable.

Raises:

EnvironmentNotWritableError -- If the check returns False.

assert_not_frozen() None#

Check whether the environment path is a valid conda environment and is not marked as frozen (as per CEP 22).

Raises:

EnvironmentIsFrozenError -- If the environment is marked as frozen.

validate_path(expand_path: bool = False) None#

Validate the path of the environment.

It runs the following checks:

  • Make sure the path does not contain : or ; (OS-dependent).

  • Disallow immediately nested environments (e.g. $CONDA_ROOT and $CONDA_ROOT/my-env).

  • Warn if there are spaces in the path.

Parameters:

expand_path -- Whether to process ~ and environment variables in the string. The expanded value will replace .prefix_path.

Raises:

CondaValueError -- If the environment contains :, ;, or is nested.

validate_name(allow_base: bool = False) None#

Validate the name of the environment.

Parameters:

allow_base -- Whether to allow base as a valid name.

Raises:

CondaValueError -- If the name is protected, or if it contains disallowed characters (/, ` , `:, #).

property created: datetime.datetime | None#

Returns the time when the environment was created, as evidenced by the conda-meta directory creation time (if available). Falls back to conda-meta/created_at.

This may return None in the following cases:

  • If the environment doesn't exist.

  • If the creation time cannot be determined. This can happen for existing environments created with conda versions before 25.11 in Linux systems, where the 'birthtime' metadata is only available in kernels 4.11+, and not implemented in Python as of Python 3.14.

property last_modified: datetime.datetime | None#

Returns the time when the environment was last modified, as evidenced by the conda-meta/history file modification time. If the environment does not exist, returns None.

size() int#

Compute the total size of a conda environment prefix.

This aggregates the installed size of all packages in the environment, plus the size of non-manifest files under conda-meta (history, markers, etc.)

Returns:

Total size in bytes.

load() None#
reload() Self#
_get_json_fn(prefix_record: conda.models.records.PrefixRecord) str#
insert(prefix_record: conda.models.records.PrefixRecord, remove_auth: bool = True) None#
remove(package_name: str) None#
get(package_name: str, default: T = NULL) conda.models.records.PackageRecord | T#
iter_records() collections.abc.Iterable[conda.models.records.PrefixRecord]#
iter_records_sorted() collections.abc.Iterable[conda.models.records.PrefixRecord]#
map_records() frozendict.frozendict[str, conda.models.records.PrefixRecord]#

Map the records to a frozendict of name -> record.

Returns:

A mapping of name -> record.

all_subdir_urls() set[str]#
query(package_ref_or_match_spec: conda.models.records.PackageRecord | conda.models.match_spec.MatchSpec | str) collections.abc.Iterable[conda.models.records.PrefixRecord]#
get_conda_packages() list[conda.models.records.PrefixRecord]#

Get conda packages sorted alphabetically by name.

Returns:

Sorted conda package records

get_python_packages() list[conda.models.records.PrefixRecord]#

Get Python packages (installed via pip) sorted alphabetically by name.

Returns:

Sorted Python package records

property _prefix_records: dict[str, conda.models.records.PrefixRecord] | None#
_load_single_record(prefix_record_json_path: conda.common.path.PathType) None#
_get_environment_state_file() dict[str, dict[str, str]]#
_write_environment_state_file(state: dict[str, dict[str, str]]) None#
_ensure_no_reserved_env_vars(env_vars_names: collections.abc.Iterable[str]) None#

Ensure that the set of env_var_names does not contain any reserved env vars. Will raise an OperationNotAllowed if a reserved env var is found.

get_environment_env_vars() dict[str, str] | dict[bytes, bytes]#
set_environment_env_vars(env_vars: dict[str, str]) dict[str, str] | None#
unset_environment_env_vars(env_vars: list[str]) dict[str, str] | None#
set_creation_time() None#

Writes a .creation-time file in conda-meta with the current timestamp, meant to be used by .created property as a fallback.

set_nonadmin() None#

Creates $PREFIX/.nonadmin if sys.prefix/.nonadmin exists (on Windows).

get_pinned_specs() tuple[conda.models.match_spec.MatchSpec]#

Find pinned specs from file and return a tuple of MatchSpec.

get_conda_anchor_files_and_records(site_packages_short_path: conda.common.path.PathType, python_records: collections.abc.Iterable[conda.models.records.PrefixRecord]) dict[conda.common.path.PathType, conda.models.records.PrefixRecord]#

Return the anchor files for the conda records of python packages.

delete_prefix_from_linked_data(path: str | os.PathLike | pathlib.Path) bool#

Here, path may be a complete prefix or a dist inside a prefix