It seems that non-python resources are included in python distribution packages one of 4 ways:
- Manifest.in file (I'm not sure when this is preferred over package_data or data_files)
- package_data in
setup.py
(for including resources within python import packages) - data_files in
setup.py
(for including resources outside python import packages) - something called
setuptools-scm
(which I believe uses your version control system to find resources instead of manifest.in or something)
Which of these are accessible from
importlib.resources
?
(It is my understanding thatimportlib.resources
is the preferred way to access such resources.) If any of these are not accessible viaimportlib.resources
, then how could/should one access such resources?Other people online have been scolded for the suggestion of using
__file__
to find the path to a resource because installed wheel distributions may be stored as zip files and therefore there won't even be a proper path to your resources. When are wheels extracted into site-packages and when do they remain zipped?
All of (1)-(3) will put files into your package (don't know about (4)). At runtime,
importlib.resources
will then be able to access any data in your package. At least with Python 3.9, which can access resources in subdirectories. Before, you had to make each subdirectory a package by adding a__init__
.As for why not use
__file__
: Pythons import system has some weird ways to resolve packages. For example it can look them up in a zip file if you use Zipapp. You may even have a custom loader for a package you are asked to load some resources from. Who knows where those resources are located? Ans: importlib.resources.Afaik, wheels are not a contender as those are unpacked.