Skip to content

Switch from pkg_resources (deprecated) to importlib - #1332

Merged
lee1043 merged 3 commits into
PCMDI:mainfrom
xylar:fix-pkg-resources
Oct 13, 2025
Merged

Switch from pkg_resources (deprecated) to importlib#1332
lee1043 merged 3 commits into
PCMDI:mainfrom
xylar:fix-pkg-resources

Conversation

@xylar

@xylar xylar commented Oct 13, 2025

Copy link
Copy Markdown
Contributor

This pull request updates the way package resources are located in the pcmdi_metrics package by replacing the deprecated pkg_resources with importlib.metadata. The changes affect both the resource location logic and related tests, ensuring compatibility with modern Python packaging practices.

Migration from pkg_resources to importlib.metadata:

  • Updated pcmdi_metrics/resources.py to use importlib.metadata instead of pkg_resources for locating package data files, specifically replacing pkg_resources.resource_filename with metadata.distribution(...).locate_file(...). [1] [2]

Test updates for new resource location logic:

  • Refactored tests in tests/test_resources.py to mock importlib.metadata.distribution instead of pkg_resources.resource_filename and pkg_resources.Requirement.parse, including the use of a dummy distribution object for resource location. [1] [2]

@xylar

xylar commented Oct 13, 2025

Copy link
Copy Markdown
Contributor Author

@lee1043, this is also ready for review. Less critical for E3SM-Unified but a good idea to take care of any deprecation warnings before they start breaking things.

@forsyth2

Copy link
Copy Markdown

For reference, this relates to the observations at E3SM-Project/zppy-interfaces#25 (comment)

@lee1043
lee1043 self-requested a review October 13, 2025 17:30
@lee1043

lee1043 commented Oct 13, 2025

Copy link
Copy Markdown
Contributor

@xylar thank you for the PR!

Confirmed that create_land_sea_mask, which use resources of from pcmdi_metrics import resources, works fine with this PR.

from pcmdi_metrics.utils import create_land_sea_mask, create_target_grid
grid = create_target_grid(-90, 90, 0, 360, target_grid_resolution="5x5")
mask = create_land_sea_mask(grid)
mask.plot()
output

Also, the Demo 1b mean climate notebook that include mean_lcimate_driver.py uses resources works fine too. Merging the PR now.

@lee1043
lee1043 merged commit 4957563 into PCMDI:main Oct 13, 2025
7 checks passed
@xylar
xylar deleted the fix-pkg-resources branch October 13, 2025 18:29
@xylar

xylar commented Oct 13, 2025

Copy link
Copy Markdown
Contributor Author

Thanks @lee1043!

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.

3 participants