【问题标题】:How to include external shell scripts with bdist_wheel in python?如何在 python 中包含带有 bdist_wheel 的外部 shell 脚本?
【发布时间】:2020-10-25 20:24:56
【问题描述】:

我一直在尝试解决此错误,但找不到解决方法。我确定这只是我的配置中的一些愚蠢的小错误,但无法弄清楚...请参阅https://github.com/tovrleaf/git-utils中的代码库

问题

通过在项目根目录中为项目中的项目运行pip3 uninstall -y guts && python setup.py bdist_wheel && pip3 install dist/guts-*.whl && guts branch list-merged,它会返回错误`FileNotFoundError: [Errno 2] No such file or directory: '/Users/.../.local/share/virtualenvs/git- utils-.../lib/python3.8/site-packages/gutscli/services/../../scripts/branch-list.sh'

由于某种原因,安装包中不包含所需的 shell 脚本,即使它显示在安装输出复制 build/scripts-3.8/branch-list.sh -> build/bdist.macosx-10.15-x86_64/wheel /guts-0.0.0.data/script

运行 ./src/gutscli/guts.py 分支列表合并工作正常!

我什至在我自己的个人存储库中创建了关于它的问题。 https://github.com/tovrleaf/git-utils/issues/1

【问题讨论】:

  • 您想将非 Python (.sh) 文件添加到您的轮子,对吗?
  • 您能否使用下面的代码示例包含文件?
  • 嗨,谢谢我让它工作了:) 实际问题是我使用的是 bdist_wheel 而不是 sdist。但是你的回答给了我很好的指导!非常感谢
  • 它也适用于bdist_wheel。我总是并且只使用轮子进行分发,包括外部文件可以很好地与下面的setup.py 配合使用:)

标签: python github pip


【解决方案1】:

您可以使用setup.py package_data 将任何文件添加到您的转盘。 使用我在以下示例中提供的辅助函数,您还可以指定递归添加的文件夹(例如 Web 面板源文件夹)。

在你的setup.py:

import setuptools
from shutil import rmtree
import os


def package_files(directories: list):
    """
    This function will return the path of all files in the directories
    recursively, as setup.py package_data cannot handle * placeholders
    """
    paths = []
    for directory in directories:
        for (path, directories, filenames) in os.walk(directory):
            for filename in filenames:
                paths.append(os.path.join('..', path, filename))
    return paths


# add all paths that you want to add recursively to package_files()
# NOTE: this path is relative to the setup.py
my_fancy_script_files = package_files(
                            [
                                "./path/to/your/script/folder/", # my scripts
                                "./path/to/other/stuff/", # other nice stuff
                            ]
                        )

setuptools.setup(
    ...
    package_data={'': my_fancy_script_files}, # you can put various files here (as a list)
    include_package_data=True,
)

如果您还有其他问题或它不适用于您的项目,请发表评论并发布您的项目源代码树以进行进一步调试:)

【讨论】:

    猜你喜欢
    • 2021-12-17
    • 2023-01-06
    • 2019-07-30
    • 2013-07-05
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    相关资源
    最近更新 更多