Skip to content

Conversation

FFY00
Copy link
Member

@FFY00 FFY00 commented Oct 23, 2020

Fixes #68

FFY00 added 3 commits October 23, 2020 12:39
That is changing, we want to add support for resources in namespace
packages.

Signed-off-by: Filipe Laíns <[email protected]>
@anthrotype
Copy link

anthrotype commented Nov 25, 2020

I don't know how the this project exactly relates to the built-in importlib.resources (I see in the README that you recommend to use the buillt-in module for Python >= 3.9), but I wonder: does the built-in importlib.resources in Python 3.9 also supports namespace packages? Or do I need to use this importlib_resources "backport" to get this feature even on Python 3.9? (which would make the backport more advanced the the real thing). Thanks

@jaraco
Copy link
Member

jaraco commented Nov 27, 2020

This project and importlib.resources are synchronized. Commits here are ported to CPython and vice-versa. The namespace package support was only recently added and hasn't yet been ported to CPython, so if you want to support namespace packages, you'll want to rely on importlib_resources on CPython 3.9 or earlier. Most likely, the namespace package support will land in Python 3.10.

@anthrotype
Copy link

Makes sense, thank you for the clarification

cutoffthetop pushed a commit to robert-koch-institut/mex-model that referenced this pull request Aug 21, 2025
Fix Python 3.9 compatibility issue with importlib.resources

Fixes TypeError: expected str, bytes or os.PathLike object, not NoneType
when importing mex.model in Python 3.9 due to namespace package issues
with importlib.resources.files().

### Changes

  - Added importlib_resources fallback for Python < 3.10 compatibility
  - Added error handling with pkgutil fallback for namespace packages
- Updated dependencies to include importlib-resources>=1.3 for Python <
3.10

### Technical Details

The issue occurred because spec.origin is None for namespace packages in
Python 3.9, causing pathlib.Path() to fail. The fix:

1. Uses importlib_resources backport which has proper namespace package
support
2. Provides fallback using pkgutil.iter_modules() when primary approach
fails

### Files Changed

- mex/model/__init__.py - Added fallback logic and refactored resource
loading
  - pyproject.toml - Added conditional dependency on importlib-resources

### References
- [Add support for resources in namespace
packages](python/importlib_resources#196) -
importlib_resources backport library
- Python 3.9's built-in `importlib.resources` lacks namespace package
support, requiring the backport library for compatibility
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.

Unable to retrieve resources from a namespace package
3 participants