An example project demonstrating various ways to access data files in Python package

packaging, resources
pip install resources-example==0.2


This project shows how to package data files within a Python distribution, and has some example code for reading the data files. To build this distribution, create a venv with setuptools, wheel, and pep517 installed, then execute the latter as a module:

python -m .

The distributions (an sdist .tar.gz and a bdist .whl) will be written to ./dist/ subdirectory. To test it out, install the distribution and run the console script resources-example.

Here's a compatibility summary of the five approaches demonstrated:

Module Description In stdlib? Works on Py2? Works on Py3? Works in zipfile? Run as script?* os.path.join yes yes yes no yes pkgutil yes yes yes yes no pkg_resources no yes yes yes yes importlib.resources yes no yes (3.7+) yes yes importlib_resources no yes yes yes yes

* "Run as script" means executing the submodule directly, e.g. python myapp/ Note that Guido considers this an anti-pattern

If you are interested in creating an executable zip from source, you can use stdlib zipapp utility (Python 3.5+):

python3 -m zipapp --compress /path/to/resources-example --main="myapp:main"

And now you can run the zip directly with the interpreter (any Python version):