Skip to content

Replace pkg_resources with importlib.resources. - #3823

Open
Natim wants to merge 4 commits into
Pylons:mainfrom
Natim:fix/pkg_resources
Open

Natim wants to merge 4 commits into
Pylons:mainfrom
Natim:fix/pkg_resources

Conversation

@Natim

@Natim Natim commented Aug 4, 2026

Copy link
Copy Markdown

Summary

Fixes #3731.

Removes all runtime use of pkg_resources from Pyramid and replaces it with importlib.resources. This drops the setuptools < 82 runtime pin and lets Pyramid work with modern setuptools releases where pkg_resources is deprecated or unavailable.

Asset overrides no longer hook into setuptools' global loader adapter (OverrideProvider / register_loader_type). Instead, Pyramid's own asset APIs look up IPackageOverrides from the application registry explicitly.

What changed

Resource access

  • Added pyramid.path.ref_filename() and pyramid.path.resource_filename() as helpers for filesystem access to package resources (including extraction from zipped distributions via importlib.resources.as_file()).
  • Rewrote PkgResourcesAssetDescriptor and PackageAssetSource to use importlib.resources.files().
  • Updated package_path(), static_view, and abspath_from_asset_spec() to go through the new resolution path.

Asset overrides

  • Removed OverrideProvider, pkg_resources.register_loader_type(), and the package.__loader__ = self hack in PackageOverrides.
  • AssetResolver.resolve() now queries IPackageOverrides from the registry and passes overrides into PkgResourcesAssetDescriptor.
  • static_view uses AssetResolver so overrides still work for static file serving.

Module layout

  • Moved Resolver, AssetResolver, and DottedNameResolver to the new pyramid.resolver module.
  • Re-exported from pyramid.path for backward compatibility.

Dependencies

  • Removed setuptools from runtime dependencies in pyproject.toml.

Backward incompatibilities

  • Asset overrides are no longer transparent to pkg_resources callers. Code that calls pkg_resources.resource_filename() (or related APIs) directly will not see overrides registered via config.override_asset(). Overrides apply only through Pyramid's asset APIs (AssetResolver, static_view, abspath_from_asset_spec, etc.).
  • IPackageOverrides no longer extends IPEP302Loader. The PEP 302 loader passthrough methods on PackageOverrides have been removed.
  • PackageOverrides no longer mutates package.__loader__.

Test plan

  • Full test suite passes locally (2618 tests)
  • Verified with setuptools>=82 installed (tested with 83.0.0)
  • Added tests/test_resolver.py and tests/pkgs/assets/ fixtures
  • Updated tests/test_path.py and tests/test_config/test_assets.py (removed OverrideProvider and pkg_resources mocks)
  • Manual check: override_asset() still works for static views, i18n translation dirs, and cache busters
  • Manual check: apps installed from wheels/eggs can still resolve template and static paths

Documentation

  • Updated docs/narr/assets.rst, docs/narr/extending.rst, docs/narr/environment.rst, and docs/glossary.rst
  • Added entries to CHANGES.rst under Features and Backward Incompatibilities

@Natim
Natim force-pushed the fix/pkg_resources branch 3 times, most recently from 5b9a6b1 to 6c008a4 Compare August 4, 2026 09:46
Rémy Hubscher added 2 commits August 5, 2026 15:38
Asset overrides now resolve through Pyramid's own APIs via explicit
IPackageOverrides registry lookup, removing the setuptools runtime
dependency and the OverrideProvider loader hook.
Use lazy re-exports in pyramid.path to break the resolver circular
import for docs autodoc, fix Windows path expectations, restore full
coverage for override and re-export code paths, and remove an unused
test import.
@Natim
Natim force-pushed the fix/pkg_resources branch from 6c008a4 to 14a816f Compare August 5, 2026 13:39
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.

pkg_resources is deprecated

1 participant