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

后端 未结 8 1887
深忆病人
深忆病人 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 17:41

    If you don't have any problem with getting your setup.py code dirty use distutils.dir_util.copy_tree.
    The whole problem is how to exclude files from it.
    Heres some the code:

    import os.path
    from distutils import dir_util
    from distutils import sysconfig
    from distutils.core import setup
    
    __packagename__ = 'x' 
    setup(
        name = __packagename__,
        packages = [__packagename__],
    )
    
    destination_path = sysconfig.get_python_lib()
    package_path = os.path.join(destination_path, __packagename__)
    
    dir_util.copy_tree(__packagename__, package_path, update=1, preserve_mode=0)
    

    Some Notes:

  • This code recursively copy the source code into the destination path.
  • You can just use the same setup(...) but use copy_tree() to extend the directory you want into the path of installation.
  • The default paths of distutil installation can be found in it's API.
  • More information about copy_tree() module of distutils can be found here.

提交回复
热议问题