Python - 递归地在setup.py中包含包数据

Cyk*_*ker 6 python setuptools

是否可以配置setup.py以便递归地包含包数据?

例如,是否有相当于:

setup(...,
      packages=['mypkg'],
      package_data={'mypkg': ['data/*.dat']},
      )
Run Code Online (Sandbox Code Playgroud)

它只是指定文件夹(可能有一些额外的选项)?

setup(...,
      packages=['mypkg'],
      package_data={'mypkg': ['data']},
      )
Run Code Online (Sandbox Code Playgroud)

例子来自:

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

hoe*_*ing 6

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},
)
Run Code Online (Sandbox Code Playgroud)

Original answer, not working ATM

Use shell globs:

setup(
    ...,
    packages=['mypkg'],
    package_data={'mypkg': ['data/*.dat', 'data/**/*.dat']},
)
Run Code Online (Sandbox Code Playgroud)

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.