diff --git a/src/ansys/dpf/core/__init__.py b/src/ansys/dpf/core/__init__.py index 89711fcf5f..b33f850a3a 100644 --- a/src/ansys/dpf/core/__init__.py +++ b/src/ansys/dpf/core/__init__.py @@ -1,5 +1,4 @@ import os -import pkg_resources try: import importlib.metadata as importlib_metadata @@ -25,7 +24,7 @@ except: # pragma: no cover pass -installed = [d.project_name for d in pkg_resources.working_set] +installed = [d.metadata["Name"] for d in importlib_metadata.distributions()] check_for = ["ansys-dpf-gatebin", "ansys-dpf-gate", "ansys-grpc-dpf"] if any([c in installed for c in check_for]): raise ImportError(f"Error during import of ansys-dpf-core:\n" diff --git a/src/ansys/dpf/gate/load_api.py b/src/ansys/dpf/gate/load_api.py index 4faa597a79..bbaf1ee251 100644 --- a/src/ansys/dpf/gate/load_api.py +++ b/src/ansys/dpf/gate/load_api.py @@ -1,7 +1,12 @@ import os +import subprocess # nosec B404 +import sys + import packaging.version -import pkg_resources -import importlib +try: + import importlib.metadata as importlib_metadata +except ImportError: # Python < 3.10 (backport) + import importlib_metadata as importlib_metadata from ansys.dpf.gate.generated import capi from ansys.dpf.gate import utils, errors from ansys.dpf.gate._version import __ansys_version__ @@ -74,19 +79,27 @@ def _find_latest_ansys_versions(): def _paths_to_dpf_server_library_installs() -> dict: path_per_version = {} - installed_packages = pkg_resources.working_set - for i in installed_packages: - if "ansys-dpf-server" in i.key: - file_name = pkg_resources.to_filename(i.project_name.replace("ansys-dpf-", "")) - try: - module = importlib.import_module("ansys.dpf." + file_name) - path_per_version[ - packaging.version.parse(module.__version__) - ] = module.__path__[0] - except ModuleNotFoundError: - pass - except AttributeError: - pass + for d in importlib_metadata.distributions(): + distribution_name = d.metadata["Name"] + if "ansys-dpf-server" in distribution_name: + # Cannot use the distribution.files() as those only list the files in the site-packages, + # which for editable installations does not necessarily give the actual location of the + # source files. It may rely on a Finder, which has to run. + # The most robust way of resolving the location is to let the import machinery do its + # job, using importlib.import_module. We do not want however to actually import the + # server libraries found, which is why we do it in a subprocess. + package_path = subprocess.check_output( # nosec B603 + args=[ + sys.executable, + "-c", + f"""import importlib +print(importlib.import_module('ansys.dpf.server{distribution_name[16:]}'.replace('-', '_')).__path__[0])""", + ], + text=True, + ).rstrip() + path_per_version[ + packaging.version.parse(d.version) + ] = package_path return path_per_version