There are times where you may need to include non-Python assets in your Python package for distribution. This may include data files or JSON assets from a submodule for example that your Python package needs to reference. By default, Python will not include these assets in the bdist/sdist
. You can either include an empty __init__.py
file in that submodule (even if the project isn't Python) and rely on the magic of packages=setuptools.find_packages()
in setup.py
or you can explicitly include those non-Python assets by using the following steps.
- In
setup.py
, include an array of locations to include. These are glob matching patterns. An example of this looks like the following:
package_data={
'top_level_package': [
'path/to/assets/one/*.json',
'path/to/assets/two/*.json',
],
},
- Now to properly reference these JSON files, you need to use
pkg_resources
, otherwise your package won't be able to properly find the assets (relative or full path):
data_file = pkg_resources.resource_stream('top_level_package', 'path/to/asset')
json_data = json.load(data_file)
print(json_data)
- Datafile support in setup.py
- Package Resources (referencing data)
- Including files in an sdist - I will say that my files were included in both bdist and sdist without including a
MANIFEST.in
file, your mileage may vary depending on how you package your distribution