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

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

This is necessary as the fallback is using pkg_resources which has
been deprecated and was removed in Python 3.12.
parent 58d72453
No related branches found
No related tags found
1 merge request!1521[cmake][py] Add version detection via importlib.metadata for Python >= 3.8
Checking pipeline status
......@@ -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.
Finish editing this message first!
Please register or to comment