JacKeown
JacKeown

Reputation: 2900

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

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)

Upvotes: 5

Views: 1517

Answers (1)

Wolfgang Kuehn
Wolfgang Kuehn

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

Related Questions