Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Recursively include package data in setup.py

Is it possible to configure setup.py so that package data are included recursively?

For example, is there an equivalent of this:

setup(...,
      packages=['mypkg'],
      package_data={'mypkg': ['data/*.dat']},
      )

which just specifies the folder (perhaps with some extra option)?

setup(...,
      packages=['mypkg'],
      package_data={'mypkg': ['data']},
      )

Examples taken from:

https://docs.python.org/2/distutils/setupscript.html#installing-package-data

like image 514
Cyker Avatar asked Oct 13 '16 21:10

Cyker


1 Answers

Update:

As pointed out by @AndriiZymohliad in the comments, the recursive globs are not supported by setuptools. You have to resolve the files programmatically. Example using pathlib:

from pathlib import Path

datadir = Path(__file__).parent / 'mypkg' / 'data'
files = [str(p.relative_to(datadir)) for p in datadir.rglob('*.dat')]

setup(
    ...,
    packages=['mypkg'],
    package_data={'mypkg': files},
)

Original answer, not working ATM

Use shell globs:

setup(
    ...,
    packages=['mypkg'],
    package_data={'mypkg': ['data/*.dat', 'data/**/*.dat']},
)

data/*.dat will include all .dat files placed directly in data, but not in subdirectories. data/**/*.dat will include all .dat files placed in any of the subdirectories of data (so it will include data/spam/file.dat and data/spam/eggs/other.dat etc), but it will not include any .dat files placed directly in data. Both globs are thus mutually exclusive; this is why you always need to provide both globs if you want to include any .dat file under data.

like image 134
hoefling Avatar answered Oct 29 '22 05:10

hoefling