Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
Description
Feature or enhancement
Proposal:
Theimportlib.resources
functions{open,read}_{text,binary}
,path
,is_resource
andcontents
, deprecated in 3.11 and removed in 3.13 alphas, are, anecdotally, missed by quite a few users.
They provide a simple API for simple tasks, while the full-featuredTraversable
API is better suited for complex ones -- especially for implementing new resources-aware loaders.
I'm now in a position where I can add these functions back and support them.
Their main drawback -- not allowing subdirectories -- can be solved by taking multiple path components as positional arguments, for example:
importlib.resources.read_text('modulename','subdirectory','subsubdir','resource.txt')
The additional arguments (encoding anderrors) would become keyword-only.
There is a wrinkle in this: in Python 3.9-3.11, the above would mean:
importlib.resources.read_text('modulename','subdirectory',encoding='subsubdir',errors='resource.txt',)
I believe that this is acceptable, since:
pragmatically: typical file names do not match typical encoding/errorhandler nameslawyerly: the functions have already been deprecated for 2 releases; no one is using them now, right?
However, if this is a problem, I can
[edit: This is solved by:]
- make the
encoding
argumentrequired if a text-reading function more than one path component is given. - plan to lift this limitation around 3.15.
importlib.resources.read_text('modulename','subdirectory','subsubdir','resource.txt',encoding='utf-8',)importlib.resources.read_text('modulename','resource.txt')# OKimportlib.resources.read_text('modulename','subdirectory','utf-8')# error
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://discuss.python.org/t/deprecating-importlib-resources-legacy-api/11386/29
Linked PRs
- gh-116608: Bring back importlib.resources functional API #116609
- gh-116608: Ignore UTF-16 BOM in importlib.resources._functional tests #117569
- gh-116608: Apply style and compatibility changes from importlib_metadata. #123028
- [3.13] gh-116608: Apply style and compatibility changes from importlib_resources. (GH-123028) #123051
- [3.13] gh-116608: Revert "[3.13] gh-116608: Apply style and compatibility changes from importlib_resources. (GH-123028) (#123051)" #123072
- [3.13] gh-116608: Re-apply style and compatibility changes from importlib_resources. (GH-123028) #123074
- [3.12] gh-116608: undeprecate functional importlib.resources API #132206