Skip to content

Use importlib.metadata instead of pkg_resources - #82

Merged
PetrDlouhy merged 1 commit into
pinax:masterfrom
PetrDlouhy:fix/drop-pkg-resources
Jul 28, 2026
Merged

PetrDlouhy merged 1 commit into
pinax:masterfrom
PetrDlouhy:fix/drop-pkg-resources

Conversation

@PetrDlouhy

Copy link
Copy Markdown
Collaborator

setuptools 82.0.0 removed pkg_resources, so on any environment with a current setuptools:

>>> import pinax.referrals
ModuleNotFoundError: No module named 'pkg_resources'

The import is unconditional and at module level in pinax/referrals/__init__.py. Because the package lives in INSTALLED_APPS, this breaks Django startup outright — the whole site fails to boot, not just the referrals feature.

pkg_resources is used for exactly one thing here: reading the package's own version. importlib.metadata.version() is the direct replacement.

Compatibility

importlib.metadata is stdlib from Python 3.8. This package still lists 3.7 in both the CI matrix (.github/workflows/main.yml) and the tox envlist, so I kept a fallback to pkg_resources on ImportError rather than silently dropping that support. No new dependency is introduced — the fallback uses what 3.7 environments already have.

If you have dropped 3.7 since, I am happy to squash this down to a plain one-line from importlib.metadata import version — just say the word.

Verified

Both paths were exercised against the installed 4.3.0 distribution: the importlib.metadata path and the fallback path (with importlib.metadata forced to be unimportable) each yield __version__ == "4.3.0", with default_app_config unchanged.

🤖 Generated with Claude Code

@PetrDlouhy

Copy link
Copy Markdown
Collaborator Author

Note on the red CI here — it is not caused by this change.

All six tests jobs fail during "Set up job", before actions/checkout runs, so this PR's diff is never even fetched:

This request has been automatically failed because it uses a deprecated version of actions/cache: v2.

GitHub now auto-fails any workflow referencing actions/cache v1/v2. It hits every PR on this repo equally. Lint is the one job that uses no cache — and it passed here, against this patch.

I have opened #83 to fix the CI configuration separately, so this PR stays scoped to the one-file library change.

For what it is worth, the two changes together were verified green on a fork — all 7 jobs passing on Python 3.7 through 3.12.

There is also a neat confirmation that this patch is doing its job: on the #83 branch, which does not include this fix, the Lint job gets far enough to run makemigrations --check and dies with ModuleNotFoundError: No module named 'pkg_resources' — exactly the failure this PR removes. Lint passing here and failing there differ only by this patch.

pkg_resources was removed in setuptools 82.0.0, so `import
pinax.referrals` raises ModuleNotFoundError on any environment with a
current setuptools. Since the package is in INSTALLED_APPS, this breaks
Django startup outright.

importlib.metadata is used where available (Python 3.8+), falling back
to pkg_resources on Python 3.7, which is still listed in the CI matrix
and the tox envlist. No new dependency is introduced. If 3.7 support is
dropped, the fallback can go with it and the import becomes a one-liner.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@PetrDlouhy
PetrDlouhy force-pushed the fix/drop-pkg-resources branch from d53d1db to 88e1b95 Compare July 28, 2026 07:57
@PetrDlouhy
PetrDlouhy merged commit d70daf0 into pinax:master Jul 28, 2026
7 checks passed
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

Successfully merging this pull request may close these issues.

1 participant