`importlib.resources` access whole directories as resources

An importlib.resources.as_file equivalent but for whole directories.

To access a directory of files in a package and load them (for example, a skybox with 6 faces), one would need to use as_file 6 times with 6 context managers. Moreover, if the API required a path to a folder that contained the 6 images, this would require manual extraction.

2 Likes

What’s annoying is that this is both supported and easy to do in importlib.resources’s now-deprecated (but still usable) predecessor pkg_resources:

resource_filename(package_or_requirement, resource_name)

[…] If the named resource is a directory, then all resources within that directory (including subdirectories) are also extracted. […]

Then in importlib.resources, it’s explicitly not supported for some reason…

Does anyone know why this feature was dropped? It would be good to know so that if it’s something fundamental, nobody will waste time writing a PR (and conversely if the only reason is “nobody got around to implementing it yet”).

This seemed like it might be related at first glance, but is actually just about files inside resource directories, not the directories themselves.

1 Like

You can call as_file() on a directory resource. I’m not sure what version that started in, but you can use the latest importlib_resources to ensure that behavior is available for any Python version.

2 Likes

@effigies I just realized that while looking at the source to see how difficult it would be to implement :person_facepalming: Unless you want to do it, I’ll write an issue & PR to reflect this possibility in the docs (which right now state, wrongly, that as_file only accepts a Traversable representing a file).

1 Like

I’m not sure what version that started in.

There’s a more fine-grained changelog in the history for importlib_resources, where you can find support for directories added in 5.9.0, which was first included in Python 3.12.

Does anyone know why this feature was dropped?

The authors of importlib resources sought to re-write the functionality from scratch in order to shed assumptions and simplify the implementation while integrating it with the Python import system.

It wasn’t until importlib_resources#58 was filed that the maintainers were even considering what it would take to support directories.

It was in fact the lack of support for directories that got me involved with the project and led to the API rewrite that landed in 3.9.

I’ll write an issue & PR to reflect this possibility in the docs.

Thanks for catching this issue and I welcome the fix. Please include me in the review. There have been problems in the docs, due in part to the dual implementations and the fact that some of the docs only exist in CPython and there are no generated API docs.

3 Likes

Issue: importlib.resources.as_file docs should mention it can provide whole directories · Issue #107732 · python/cpython · GitHub
PR: gh-107732: Mention dir support in importlib.resources docs by smheidrich · Pull Request #107734 · python/cpython · GitHub