Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider importlib.metadata over pkg_resources for newer Python versions #1184

Closed
dvzrv opened this issue Sep 21, 2021 · 3 comments
Closed

Comments

@dvzrv
Copy link

dvzrv commented Sep 21, 2021

Hi! I'm packaging radicale for Arch Linux. We are currently trying to clean up packages that require setuptools during runtime (mainly those packages did so due to definition of entry_points, which are now properly handled without setuptools).

In newer Python versions it is more viable to rely on importlib.metadata to retrieve a package version. Would it be possible to move towards attempting to use importlib.metadata if pkg_resources is not found?

For posterity, here is an example of where pkg_resources is used in the code base (pulling in setuptools during runtime):

VERSION = pkg_resources.get_distribution("radicale").version

@jonsger
Copy link

jonsger commented Apr 1, 2022

Hi,

I'm trying to update the GNU Guix package of radicale, when I stumbled over this issue. I got 2 of 3 uses of pkg_resources removed:

diff --git a/radicale/__init__.py b/radicale/__init__.py
index 1f10773..760015b 100644
--- a/radicale/__init__.py
+++ b/radicale/__init__.py
@@ -29,13 +29,13 @@ import os
 import threading
 from typing import Iterable, Optional, cast
 
-import pkg_resources
+import importlib.metadata
 
 from radicale import config, log, types
 from radicale.app import Application
 from radicale.log import logger
 
-VERSION: str = pkg_resources.get_distribution("radicale").version
+VERSION: str = importlib.metadata.version("radicale")
 
 _application_instance: Optional[Application] = None
 _application_config_path: Optional[str] = None
diff --git a/radicale/storage/__init__.py b/radicale/storage/__init__.py
index db6a871..8109b2b 100644
--- a/radicale/storage/__init__.py
+++ b/radicale/storage/__init__.py
@@ -29,7 +29,7 @@ from hashlib import sha256
 from typing import (Iterable, Iterator, Mapping, Optional, Sequence, Set,
                     Tuple, Union, overload)
 
-import pkg_resources
+import importlib.metadata
 import vobject
 
 from radicale import config
@@ -41,7 +41,7 @@ INTERNAL_TYPES: Sequence[str] = ("multifilesystem", "multifilesystem_nolock",)
 
 CACHE_DEPS: Sequence[str] = ("radicale", "vobject", "python-dateutil",)
 CACHE_VERSION: bytes = "".join(
-    "%s=%s;" % (pkg, pkg_resources.get_distribution(pkg).version)
+    "%s=%s;" % (pkg, importlib.metadata.version(pkg))
     for pkg in CACHE_DEPS).encode()
 
 

But

self.folder = pkg_resources.resource_filename(

seems to be a bit more tricky to replace. I tried some different approaches, but all failed. The relevant migration guide can be found here: https://importlib-resources.readthedocs.io/en/latest/migration.html#pkg-resources-resource-filename

@Unrud
Copy link
Collaborator

Unrud commented Apr 3, 2022

But ... seems to be a bit more tricky to replace. I tried some different approaches, but all failed.

importlib.resources.files can be used to get Traversable. The function serve_folder must be converted to work with Traversable instead of the file system (the Traversable can be a regular file system path but it can also point inside an compressed egg file).

The main reason for not using it, is that all required parts are only available in Python >= 3.9.

@dvzrv
Copy link
Author

dvzrv commented Apr 19, 2022

Awesome stuff! Thank you! 🎉

cheat-sc added a commit to cheat-sc/ttfautohint-py that referenced this issue Jun 15, 2024
Since python 3.12, pkg_resources has been moved to setuptools. Also, it is deprecated.

- Kozea/Radicale#1184
- mu-editor/mu#2485
- python/cpython#95299

Signed-off-by: Shohei Maruyama <cheat.sc.linux@outlook.com>
cheat-sc added a commit to cheat-sc/ttfautohint-py that referenced this issue Jun 15, 2024
Since python 3.12, pkg_resources has been moved to setuptools. Also, it is deprecated.

- Kozea/Radicale#1184
- mu-editor/mu#2485
- python/cpython#95299

$ python -m ttfautohint --help
Traceback (most recent call last):
  File "/usr/lib/python3.12/site-packages/ttfautohint/_version.py", line 2, in <module>
    from pkg_resources import get_distribution, DistributionNotFound
ModuleNotFoundError: No module named 'pkg_resources'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen runpy>", line 189, in _run_module_as_main
  File "<frozen runpy>", line 148, in _get_module_details
  File "<frozen runpy>", line 112, in _get_module_details
  File "/usr/lib/python3.12/site-packages/ttfautohint/__init__.py", line 12, in <module>
    from ttfautohint._version import __version__
  File "/usr/lib/python3.12/site-packages/ttfautohint/_version.py", line 4, in <module>
    except (ImportError, DistributionNotFound):
                         ^^^^^^^^^^^^^^^^^^^^
NameError: name 'DistributionNotFound' is not defined

Signed-off-by: Shohei Maruyama <cheat.sc.linux@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants