如何将python软件包的data_files安装到主目录

use*_*272 5 python pip twine

这是我的setup.py

setup(
    name='shipane_sdk',

    version='1.0.0.a5',

    # ...

    data_files=[(os.path.join(os.path.expanduser('~'), '.shipane_sdk', 'config'), ['config/scheduler-example.ini'])],

    # ...
)
Run Code Online (Sandbox Code Playgroud)

打包和上传命令:

python setup.py sdist
python setup.py bdist_wheel --universal
twine upload dist/*
Run Code Online (Sandbox Code Playgroud)

安装命令:

pip install shipane_sdk
Run Code Online (Sandbox Code Playgroud)

但是,它不会在〜/ .shipane_sdk下安装config / scheduler-example.ini

点子文件说:

setuptools允许绝对的“ data_files”路径,从sdist安装时,pip会将其视为绝对路径。从车轮分配上安装时,情况并非如此。车轮不支持绝对路径,并且最终会相对于“站点包”进行安装。有关讨论,请参见第92期。

您知道如何从sdist安装吗?

Jul*_*ska 1

这个问题有多种解决方案,但打包工具工作的不一致程度非常令人困惑。不久前,我发现以下解决方法对于 sdist 最适合我(请注意,它不适用于轮子!):

  1. 不要使用 data_files,而是使用 MANIFEST.in 将文件附加到包中,在您的情况下,它可能如下所示:

    include config/scheduler-example.ini
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用 setup.py 中的以下代码片段“手动”将文件复制到所选位置:

    if 'install' in sys.argv:
        from pkg_resources import Requirement, resource_filename
        import os
        import shutil
    
        # retrieve the temporary path where the package has been extracted to for installation
        conf_path_temp = resource_filename(Requirement.parse(APP_NAME), "conf")
    
        # if the config directory tree doesn't exist, create it
        if not os.path.exists(CONFIG_PATH):
            os.makedirs(CONFIG_PATH)
    
        # copy every file from given location to the specified ``CONFIG_PATH``
        for file_name in os.listdir(conf_path_temp):
            file_path_full = os.path.join(conf_path_temp, file_name)
            if os.path.isfile(file_path_full):
                shutil.copy(file_path_full, CONFIG_PATH)
    
    Run Code Online (Sandbox Code Playgroud)

就我而言,“conf”是包含我的数据文件的包中的子目录,它们应该安装到 CONFIG_PATH 中,类似于 /etc/APP_NAME