Add folders and subfolders outside of main module using setuptools(setup.py)

前端 未结 1 1650
滥情空心
滥情空心 2021-01-22 08:45

So it is yet another similar looking but different question than setuptools: adding additional files outside package and Including non-Python files with setup.py. I have structu

相关标签:
1条回答
  • 2021-01-22 09:34

    I believe something like the following could get you there:

    .
    ├── data
    │   ├── file.txt
    │   └── foo
    │       ├── bar
    │       │   └── file.txt
    │       └── file.txt
    ├── MANIFEST.in
    ├── setup.cfg
    ├── setup.py
    └── src
        └── thing
            ├── __init__.py
            └── nothing
                └── __init__.py
    

    MANIFEST.in

    # ...
    recursive-include data *.txt
    

    setup.py

    #!/usr/bin/env python3
    
    import pathlib
    import setuptools
    
    def main():
        data_packages = [
            'thing/{}'.format(p).replace('/', '.')
            for p
            in pathlib.Path('data').glob('**')
        ]
        packages = setuptools.find_packages(where='src')
        package_dir = {
            'thing': 'src/thing',
            'thing.data': 'data'
        }
    
        setuptools.setup(
            # see 'setup.cfg'
            packages=packages + data_packages,
            package_dir=package_dir,
            include_package_data=True,
        )
    
    if __name__ == '__main__':
        main()
    

    The *.txt are packaged as package data under the thing.data package of the Python project. The data can be read for example like this:

    python3 -c "import pkgutil; print(pkgutil.get_data('thing', 'data/foo/bar/file.txt').decode())"
    
    0 讨论(0)
提交回复
热议问题