pkg_resources
PKGL资源
(部分)
setuptools
包)提供标记评估功能。
In [1]: from pkg_resources import evaluate_marker
In [2]: evaluate_marker('sys_platform == "darwin"')
Out[2]: True
In [3]: evaluate_marker('python_version > "3.7"')
Out[3]: False
In [4]: evaluate_marker('implementation_name == "cpython"')
Out[4]: True
In [5]: evaluate_marker('garbage')
Traceback (most recent call last):
File "/Users/hoefling/Library/Python/3.7/lib/python/site-packages/IPython/core/interactiveshell.py", line 2961, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-69434540d2ec>", line 1, in <module>
evaluate_marker('garbage')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 1372, in evaluate_marker
raise SyntaxError(e)
File "<string>", line unknown
SyntaxError: Invalid marker: 'garbage', parse error at 'garbage'
等。
packaging
在引擎盖下,
pkg_resources.evaluate_marker
使用
packaging.markers.Marker.evaluate
从
packaging
打包,因此您可以使用它:
In [6]: from packaging.markers import Marker
In [7]: Marker('"linux" in sys_platform').evaluate()
Out[7]: False