How to add package data recursively in Python setup.py?

distutils, python, setup.py

Solution

- Use Setuptools instead of distutils.

- Use data files instead of package data. These do not require `__init__.py`.

Generate the lists of files and directories using standard Python code, instead of writing it literally:

data_files = []
directories = glob.glob('data/subfolder?/subfolder??/')
for directory in directories:
    files = glob.glob(directory+'*')
    data_files.append((directory, files))
# then pass data_files to setup()

Problem

I have a new library that has to include a lot of subfolders of small datafiles, and I'm trying to add them as package data. Imagine I have my library as so: ``` library - foo.py - bar.py data subfolderA subfolderA1 subfolderA2 subfolderB subfolderB1 ... ``` I want to add all of the data in all of the subfolders through `setup.py`, but it seems like I manually have to go into every single subfolder (there are 100 or so) and add an `__init__.py` file. Furthermore, will `setup.py` find these files recursively, or do I need to manually add all of these in `setup.py` like: ``` package_data={ 'mypackage.data.folderA': ['*'], 'mypackage.data.folderA.subfolderA1': ['*'], 'mypackage.data.folderA.subfolderA2': ['*'] }, ``` I can do this with a script, but seems like a super pain. How can I achieve this in `setup.py`? PS, the hierarchy of these folders is important because this is a database of material files and we want the file tree to be preserved when we present them in a GUI to the user, so it would be to our advantage to keep this file structure intact.

Original source

Related problems