Reputation: 2900
It seems that non-python resources are included in python distribution packages one of 4 ways:
setup.py
(for including resources within python import packages)setup.py
(for including resources outside python import packages)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 that importlib.resources
is the preferred way to access such resources.) If any of these are not accessible via importlib.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?
Upvotes: 5
Views: 1517
Reputation: 12926
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.
Upvotes: 2