From 774519f1628d2ed2e10ddfeba09e13aee463ef48 Mon Sep 17 00:00:00 2001 From: Jim Garrison Date: Thu, 29 Feb 2024 09:25:55 -0500 Subject: [PATCH] Migrate from `pkg_resources` to `importlib.metadata` PR #1347 added `setuptools` to `requirements-dev.txt` in an effort to make `qiskit-ibm-runtime` work with Python 3.12. However, `qiskit_ibm_runtime/api/session.py` actually imports `pkg_resources`, which is provided by setuptools, so really setuptools should have been added to [`REQUIREMENTS`](https://github.com/Qiskit/qiskit-ibm-runtime/blob/e152d122782959a04ebf2330f776d4c4e309ad14/setup.py#L21) in `setup.py` so that it is installed automatically for any downstream users of Qiskit Runtime, too. Better yet, however, is to drop this explicit dependency on setuptools, since [`pkg_resources` is deprecated](https://setuptools.pypa.io/en/latest/pkg_resources.html) in favor of `importlib`. Hence, this PR migrates from `pkg_resources` to `importlib.metadata` (there are a few more words about this [here](https://github.com/googleapis/python-api-core/issues/27#issuecomment-676563754)). My investigation into this was prompted by us trying to add Python 3.12 support to the circuit-knitting-toolbox (https://github.com/Qiskit-Extensions/circuit-knitting-toolbox/pull/429), and seeing CI errors prompted by `qiskit-ibm-runtime`'s importing of `pkg_resources`. --- qiskit_ibm_runtime/api/session.py | 4 ++-- requirements-dev.txt | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/qiskit_ibm_runtime/api/session.py b/qiskit_ibm_runtime/api/session.py index d2e0b221b..9972c1096 100644 --- a/qiskit_ibm_runtime/api/session.py +++ b/qiskit_ibm_runtime/api/session.py @@ -19,7 +19,7 @@ import sys from typing import Dict, Optional, Any, Tuple, Union from pathlib import PurePath -import pkg_resources +import importlib.metadata from requests import Session, RequestException, Response from requests.adapters import HTTPAdapter @@ -74,7 +74,7 @@ def _get_client_header() -> str: pkg_versions = {"qiskit_ibm_runtime": f"qiskit_ibm_runtime-{ibm_runtime_version}"} for pkg_name in qiskit_pkgs: try: - version_info = f"{pkg_name}-{pkg_resources.get_distribution(pkg_name).version}" + version_info = f"{pkg_name}-{importlib.metadata.version(pkg_name)}" if pkg_name in sys.modules: version_info += "*" diff --git a/requirements-dev.txt b/requirements-dev.txt index 7236cd015..0218c540d 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -13,7 +13,6 @@ black~=22.0 coverage>=6.3 pylatexenc scikit-learn -setuptools ddt>=1.2.0,!=1.4.0,!=1.4.3 # Documentation