From 312ecc04b192e62187ff0ac4a3b35ca750bd3ed5 Mon Sep 17 00:00:00 2001 From: Peter Marheine Date: Tue, 30 Jul 2024 09:41:08 +0000 Subject: [PATCH] Replace use of pkg_resources (setuptools) Since Python 3.12, setuptools isn't included with Python and importlib is the recommended replacement, available since Python 3.8. To support back to Python 3.7, this adds a dependency on importlib_metadata which provides a backport of importlib.metadata via PyPI. --- django_downloadview/__init__.py | 4 ++-- docs/conf.py | 5 +++-- setup.py | 1 + tests/packaging.py | 13 ++++++------- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/django_downloadview/__init__.py b/django_downloadview/__init__.py index b0a9d8d..b9f9ddc 100644 --- a/django_downloadview/__init__.py +++ b/django_downloadview/__init__.py @@ -1,7 +1,7 @@ """Serve files with Django and reverse proxies.""" from django_downloadview.api import * # NoQA -import pkg_resources +import importlib_metadata #: Module version, as defined in PEP-0396. -__version__ = pkg_resources.get_distribution(__package__.replace("-", "_")).version +__version__ = importlib_metadata.version(__package__.replace("-", "_")) diff --git a/docs/conf.py b/docs/conf.py index a91a711..930c9ff 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -2,7 +2,8 @@ """django-downloadview documentation build configuration file.""" import os import re -from pkg_resources import get_distribution + +import importlib_metadata # Minimal Django settings. Required to use sphinx.ext.autodoc, because # django-downloadview depends on Django... @@ -48,7 +49,7 @@ # built documents. # The full version, including alpha/beta/rc tags. -release = get_distribution("django-downloadview").version +release = importlib_metadata.version("django-downloadview") # The short X.Y version. version = '.'.join(release.split('.')[:2]) diff --git a/setup.py b/setup.py index 1788a75..1cc269d 100644 --- a/setup.py +++ b/setup.py @@ -57,6 +57,7 @@ install_requires=[ # BEGIN requirements "Django>=2.2", + "importlib_metadata", "requests", # END requirements ], diff --git a/tests/packaging.py b/tests/packaging.py index b8fbced..1cb8393 100644 --- a/tests/packaging.py +++ b/tests/packaging.py @@ -24,22 +24,21 @@ def test_version_present(self): self.fail("django_downloadview package has no __version__.") def test_version_match(self): - """django_downloadview.__version__ matches pkg_resources info.""" + """django_downloadview.__version__ matches importlib metadata.""" try: - import pkg_resources + import importlib_metadata except ImportError: self.fail( - "Cannot import pkg_resources module. It is part of " - "setuptools, which is a dependency of " - "django_downloadview." + "Cannot import importlib_metadata module. It is a " + "dependency of django_downloadview." ) - distribution = pkg_resources.get_distribution("django-downloadview") + distribution = importlib_metadata.distribution("django-downloadview") installed_version = distribution.version self.assertEqual( installed_version, self.get_version(), "Version mismatch: django_downloadview.__version__ " - 'is "%s" whereas pkg_resources tells "%s". ' + 'is "%s" whereas importlib_metadata tells "%s". ' "You may need to run ``make develop`` to update the " "installed version in development environment." % (self.get_version(), installed_version),