Markers

One extra requirement of dealing with dependencies is the ability to specify if it is required depending on the operating system or Python version in use. The specification of dependency specifiers defines the scheme which has been implemented by this module.

Usage

>>> from packaging.markers import Marker, UndefinedEnvironmentName
>>> marker = Marker("python_version>'2'")
>>> marker
<Marker('python_version > "2"')>
>>> # We can evaluate the marker to see if it is satisfied
>>> marker.evaluate()
True
>>> # We can also override the environment
>>> env = {'python_version': '1.5'}
>>> marker.evaluate(environment=env)
False
>>> # Multiple markers can be ANDed
>>> and_marker = Marker("os_name=='a' and os_name=='b'")
>>> and_marker
<Marker('os_name == "a" and os_name == "b"')>
>>> # Multiple markers can be ORed
>>> or_marker = Marker("os_name=='a' or os_name=='b'")
>>> or_marker
<Marker('os_name == "a" or os_name == "b"')>
>>> # Markers can be also used with extras, to pull in dependencies if
>>> # a certain extra is being installed
>>> extra = Marker('extra == "bar"')
>>> # You can do simple comparisons between marker objects:
>>> Marker("python_version > '3.6'") == Marker("python_version > '3.6'")
True
>>> # You can also perform simple comparisons between sets of markers:
>>> markers1 = {Marker("python_version > '3.6'"), Marker('os_name == "unix"')}
>>> markers2 = {Marker('os_name == "unix"'), Marker("python_version > '3.6'")}
>>> markers1 == markers2
True

Reference

class packaging.markers.Environment

A dictionary that represents a Python environment as captured by default_environment(). All fields are required.

implementation_name: str

The implementation’s identifier, e.g. 'cpython'.

implementation_version: str

The implementation’s version, e.g. '3.13.0a2' for CPython 3.13.0a2, or '7.3.13' for PyPy3.10 v7.3.13.

os_name: str

The value of os.name. The name of the operating system dependent module imported, e.g. 'posix'.

platform_machine: str

Returns the machine type, e.g. 'i386'.

An empty string if the value cannot be determined.

platform_release: str

The system’s release, e.g. '2.2.0' or 'NT'.

An empty string if the value cannot be determined.

platform_system: str

The system/OS name, e.g. 'Linux', 'Windows' or 'Java'.

An empty string if the value cannot be determined.

platform_version: str

The system’s release version, e.g. '#3 on degas'.

An empty string if the value cannot be determined.

python_full_version: str

The Python version as string 'major.minor.patchlevel'.

Note that unlike the Python sys.version, this value will always include the patchlevel (it defaults to 0).

platform_python_implementation: str

A string identifying the Python implementation, e.g. 'CPython'.

python_version: str

The Python version as string 'major.minor'.

sys_platform: str

This string contains a platform identifier that can be used to append platform-specific components to sys.path, for instance.

For Unix systems, except on Linux and AIX, this is the lowercased OS name as returned by uname -s with the first part of the version as returned by uname -r appended, e.g. 'sunos5' or 'freebsd8', at the time when Python was built.

packaging.markers.EvaluateContext

A typing.Literal enumerating valid marker evaluation contexts.

Valid values for the context passed to Marker.evaluate() are:

  • "metadata" (for core metadata; default)

  • "lock_file" (for lock files)

  • "requirement" (i.e. all other situations)

alias of Literal[‘metadata’, ‘lock_file’, ‘requirement’]

exception packaging.markers.InvalidMarker

Raised when attempting to create a Marker from invalid input.

This error indicates that the given marker string does not conform to the specification of dependency specifiers.

class packaging.markers.Marker

Represents a parsed dependency marker expression.

Marker expressions are parsed according to the specification of dependency specifiers.

Parameters:

marker – The string representation of a marker expression.

Raises:

InvalidMarker – If marker cannot be parsed.

evaluate(environment=None, context='metadata')

Evaluate a marker.

Return the boolean from evaluating this marker against the environment. The environment is determined from the current Python process unless passed in explicitly.

Parameters:
  • environment (Mapping[str, str | AbstractSet[str]] | None) – Mapping containing keys and values to override the detected environment.

  • context (EvaluateContext) – The context in which the marker is evaluated, which influences what marker names are considered valid. Accepted values are "metadata" (for core metadata; default), "lock_file", and "requirement" (i.e. all other situations).

Raises:
Returns:

True if the marker matches, otherwise False.

Return type:

bool

exception packaging.markers.UndefinedComparison

Raised when evaluating an unsupported marker comparison.

This can happen when marker values are compared as versions but do not conform to the specification of version specifiers.

exception packaging.markers.UndefinedEnvironmentName

Raised when evaluating a marker that references a missing environment key.

packaging.markers.default_environment()

Return the default marker environment for the current Python process.

This is the base environment used by Marker.evaluate().

Return type:

Environment