I'm building my first python package (which I then install with pip) and I need to use some non-python files. In these answers, it is explained that I should use the pkg_resources
function. But I can't figure out a working example. Let say I have this project structure:
package_name/
----data/
--------image.png
----package_name/
--------__init__.py
--------file.py
----setup.py
----MANIFEST.in
----conf.yml
Now I want to access conf.yml
and image.png
from file.py
. How should I proceed in:
The simplest way to access these files would be to include in MANIFEST.in
global-include *.png
global-include *.yml
MANIFEST.in will only use files for a source distribution though, while setup.py will include files for binary/wheel distributions so just to be safe, inside of your setup.py
include_package_data = True,
package_data = {
'' : ['*.png'],
'' : ['*.yml'],
}
Then you can reference the specific file like so from file.py
from pkg_resources import resource_string
def foo():
pngfile = resource_string(__name__, 'data/image.png')
ymlfile = resource_string(__name__, 'conf.yml')
Notice how for the png file I've specified the directory.
This solution also does not account for files of the same extension which you may want to exclude, but those could easily be taken care of with exclude or specifying filenames rather than using the asterisk.
I know there are questions that could easily be considered duplicates, but I had trouble getting a workable example as well and after a good while badgering away myself I managed to get something to work, and this was it.