gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:32:29 26 4
gpt4 key购买 nike

这是我的 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'])],

# ...
)

打包&上传命令:

python setup.py sdist
python setup.py bdist_wheel --universal
twine upload dist/*

安装命令:

pip install shipane_sdk

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

pip 文档说:

setuptools allows absolute “data_files” paths, and pip honors them as absolute, when installing from sdist. This is not true when installing from wheel distributions. Wheels don’t support absolute paths, and they end up being installed relative to “site-packages”. For discussion see wheel Issue #92.

你知道如何从 sdist 安装吗?

最佳答案

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

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

    include config/scheduler-example.ini
  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)

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

关于python - 如何将python包的data_files安装到主目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41328318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com