Skip to content
Snippets Groups Projects
Commit 56807ac0 authored by Timo Koch's avatar Timo Koch
Browse files

Merge branch 'fix/pyversion-python-from-3.12' into 'master'

[cmake][py] Add version detection via importlib.metadata for Python >= 3.8

See merge request !1521
parents a7da5ef8 9995d691
No related branches found
No related tags found
1 merge request!1521[cmake][py] Add version detection via importlib.metadata for Python >= 3.8
Pipeline #77803 waiting for manual action
......@@ -26,14 +26,25 @@ with warnings.catch_warnings():
sys.stdout.write(module.__version__)
sys.exit(0)
if sys.version_info.major == 3 and sys.version_info.minor >= 8:
import importlib.metadata
try:
sys.stdout.write(importlib.metadata.version(modstr))
sys.exit(0)
except importlib.metadata.PackageNotFoundError:
pass
# Alternative implementation: through pip (pip itself implement pip.__version__,
# so we never get here, when checking the version of pip itself), only works if
# package name and distribution name are the same
import pkg_resources
for package in pkg_resources.working_set:
if package.project_name == modstr and package.has_version():
sys.stdout.write(package.version)
sys.exit(0)
try:
import pkg_resources
for package in pkg_resources.working_set:
if package.project_name == modstr and package.has_version():
sys.stdout.write(package.version)
sys.exit(0)
except ImportError:
pass
# Give up on this one
# We ran out of options: give up on this one
sys.exit(1)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment