Packaging python resources (Manifest.in vs package_data vs data_files)

1.2k views Asked by At

It seems that non-python resources are included in python distribution packages one of 4 ways:

  1. Manifest.in file (I'm not sure when this is preferred over package_data or data_files)
  2. package_data in setup.py (for including resources within python import packages)
  3. data_files in setup.py (for including resources outside python import packages)
  4. 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 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?

1

There are 1 answers

4
Wolfgang Kuehn On BEST ANSWER

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.