4

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:

  • file.py ?
  • setup.py ?
  • MANIFEST.in ?
Robin
  • 1,531
  • 1
  • 15
  • 35
  • Possible duplicate of [Any python function to get "data\_files" root directory?](https://stackoverflow.com/questions/14211575/any-python-function-to-get-data-files-root-directory) – phd Dec 14 '17 at 16:35
  • Thx for the link. I still think it would be great to have an explanation from scratch. Such explanation could also include how to run tests locally. – Robin Dec 14 '17 at 17:05
  • Minimal runnable published working example at: https://stackoverflow.com/questions/3596979/manifest-in-ignored-on-python-setup-py-install-no-data-files-installed/60735402#60735402 – Ciro Santilli OurBigBook.com Mar 18 '20 at 07:53

1 Answers1

5

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.

Noved
  • 68
  • 1
  • 6
  • `pkg_resources` is deprecated. Use `importlib.resources` instead. Details: https://importlib-resources.readthedocs.io/en/latest/migration.html – majkelx Aug 03 '23 at 15:02