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

Combining markers programmatically

Marker objects support the & (and) and | (or) operators for combining markers without manually constructing marker strings:

>>> from packaging.markers import Marker
>>> py3 = Marker("python_version >= '3'")
>>> linux = Marker("sys_platform == 'linux'")
>>> combined = py3 & linux
>>> str(combined)
'python_version >= "3" and sys_platform == "linux"'
>>> either = py3 | linux
>>> str(either)
'python_version >= "3" or sys_platform == "linux"'

This is equivalent to writing the combined marker string directly but is useful when building markers dynamically from separate conditions.

Added in version 26.1.

Reference

exception packaging.markers.InvalidMarker

An invalid marker was found, users should refer to PEP 508.

exception packaging.markers.UndefinedComparison

An invalid operation was attempted on a value that doesn’t support it.

exception packaging.markers.UndefinedEnvironmentName

A name was attempted to be used that does not exist inside of the environment.