Skip to content

Replace pkg_resources with importlib.resources#65

Merged
inducer merged 2 commits intomainfrom
copilot/replace-pkg-resources
Feb 15, 2026
Merged

Replace pkg_resources with importlib.resources#65
inducer merged 2 commits intomainfrom
copilot/replace-pkg-resources

Conversation

Copy link
Contributor

Copilot AI commented Feb 15, 2026

Replaces deprecated pkg_resources API with standard library importlib.resources in add_py_module().

Changes

  • codepy/libraries.py: Replaced pkg_resources.resource_filename() with importlib.resources.files()
# Before
from pkg_resources import Requirement, resource_filename
return resource_filename(Requirement.parse(name), f"{name}/include")

# After
from importlib.resources import files
return str(files(name) / "include")

Behavior is equivalent for all current usages (codepy, pyublas, hedge) where distribution name matches module name. Compatible with Python 3.10+ requirement (files() introduced in 3.9).

Original prompt

This section details on the original issue you should resolve

<issue_title>Replace pkg_resources</issue_title>
<issue_description>@copilot Replace the pkg_resources use with importlib.resources.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: inducer <352067+inducer@users.noreply.github.com>
Copilot AI changed the title [WIP] Replace pkg_resources with importlib.resources Replace pkg_resources with importlib.resources Feb 15, 2026
Copilot AI requested a review from inducer February 15, 2026 19:51
@inducer inducer marked this pull request as ready for review February 15, 2026 19:52
Copilot AI review requested due to automatic review settings February 15, 2026 19:52
@inducer inducer enabled auto-merge (squash) February 15, 2026 19:52
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates add_py_module() to stop using deprecated pkg_resources and instead use the standard-library importlib.resources to locate a Python package’s bundled include/ directory for toolchain header search paths.

Changes:

  • Replace pkg_resources.resource_filename() lookup with importlib.resources.files() for package include path resolution.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +188 to 190
from importlib.resources import files
return str(files(name) / "include")

Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

importlib.resources.files() returns a Traversable that is not guaranteed to be a real filesystem path (e.g., zip/egg installs). Converting it to str and passing it as a compiler -I include dir can yield a non-existent path and break compilation, whereas pkg_resources.resource_filename() previously extracted resources to a real path. Consider resolving the package location via importlib.util.find_spec(...).submodule_search_locations (filesystem-only, like add_numpy) or materializing the directory with importlib.resources.as_file() and ensuring the extracted path remains valid for the entire build.

Suggested change
from importlib.resources import files
return str(files(name) / "include")
from importlib.util import find_spec
spec = find_spec(name)
if spec is None:
raise RuntimeError(f"Could not find module '{name}'")
libdir = spec.submodule_search_locations
assert libdir is not None
from os.path import join
return join(libdir[0], "include")

Copilot uses AI. Check for mistakes.
@inducer inducer merged commit 61334d2 into main Feb 15, 2026
13 checks passed
@inducer inducer deleted the copilot/replace-pkg-resources branch February 15, 2026 19:57
@inducer inducer restored the copilot/replace-pkg-resources branch February 15, 2026 19:59
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.

Replace pkg_resources

2 participants