setuptools: package data folder location

前端 未结 3 1313
死守一世寂寞
死守一世寂寞 2020-11-27 11:04

I use setuptools to distribute my python package. Now I need to distribute additional datafiles.

From what I\'ve gathered fromt the setuptools documentation, I need

3条回答
  •  猫巷女王i
    2020-11-27 11:50

    I Think I found a good compromise which will allow you to mantain the following structure:

    / #root
    |- data/
    |  |- resource1
    |  |- [...]
    |- src/
    |  |- mypackage/
    |  |  |- __init__.py
    |  |  |- [...]
    |- setup.py
    

    You should install data as package_data, to avoid the problems described in samplebias answer, but in order to mantain the file structure you should add to your setup.py:

    try:
        os.symlink('../../data', 'src/mypackage/data')
        setup(
            ...
            package_data = {'mypackage': ['data/*']}
            ...
        )
    finally:
        os.unlink('src/mypackage/data')
    

    This way we create the appropriate structure "just in time", and mantain our source tree organized.

    To access such data files within your code, you 'simply' use:

    data = resource_filename(Requirement.parse("main_package"), 'mypackage/data')

    I still don't like having to specify 'mypackage' in the code, as the data could have nothing to do necessarally with this module, but i guess its a good compromise.

提交回复
热议问题