加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 13:15:37 所属栏目:Python 来源:网络整理
导读:这是我的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 sdistpython setup.py
这是我的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

点子文件说:

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(注意它不适用于轮子!):

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

include config/scheduler-example.ini

>使用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”是包中包含我的数据文件的子目录,它们应该被安装到CONFIG_PATH中,类似于/ etc / APP_NAME

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读