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

后端 未结 8 1899
深忆病人
深忆病人 2020-12-13 17:20

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:



        
8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 18:05

    @gbonetti's answer, using a recursive glob pattern, i.e. **, would be perfect.

    However, as commented by @daniel-himmelstein, that does not work yet in setuptools package_data.

    So, for the time being, I like to use the following workaround, based on pathlib's Path.glob():

    def glob_fix(package_name, glob):
        # this assumes setup.py lives in the folder that contains the package
        package_path = Path(f'./{package_name}').resolve()
        return [str(path.relative_to(package_path)) 
                for path in package_path.glob(glob)]
    

    This returns a list of path strings relative to the package path, as required.

    Here's one way to use this:

    setuptools.setup(
        ...
        package_data={'my_package': [*glob_fix('my_package', 'my_data_dir/**/*'), 
                                     'my_other_dir/some.file', ...], ...},
        ...
    )
    

    The glob_fix() can be removed as soon as setuptools supports ** in package_data.

提交回复
热议问题