【问题标题】:Create python wheel based on platform基于平台创建python轮子
【发布时间】:2020-05-06 12:10:22
【问题描述】:

我正在尝试将我的 python 代码打包到 wheel 文件中。我的代码依赖项因平台而异。 例如,在 Windows 中,我需要 psycopg2,而在 linux 中,我需要 psycopg2-binary

我在我的项目中创建了两个单独的文件:requirements.txtrequirements_linux.txt。 下面是我的详细资料 Setup.py

requirement_file='requirements.txt'
if sys.platform == 'linux':
    requirement_file = 'requirements_linux.txt'
here = os.path.abspath(os.path.dirname(__file__))
# setup method inside setup.py

setup(
...
...
install_requires=open(os.path.join(here,requirement_file)).readlines(),
...
)

现在我正在使用来自 Windows 系统的命令 python setup.py bdist_wheel 命令创建轮文件。但是上面的代码似乎不起作用。当我在 linux 环境中运行 wheel 文件时,它会搜索 psycopg2 而不是 psycopg2-binary。我错过了什么吗?

如何为 linux 或 Mac 等其他平台创建 wheel 文件并在其中具有单独的依赖项?

【问题讨论】:

  • “似乎不起作用”不是一个很有帮助的问题描述。如果您遇到错误,请考虑将完整的错误堆栈添加到您的问题中。
  • 我已经更新了这个问题。请检查。

标签: python python-3.x python-wheel python-packaging


【解决方案1】:

作为 wheel 分发的 Python 项目不包含 setup.py 文件。因此它不能在安装时运行。

setuptools 指定平台特定依赖项的正确方法如下:

setuptools.setup(
    # ...
    install_requires=[
        "LinuxOnlyDependency ; platform_system=='Linux'",
        "WindowsOnlyDependency ; platform_system=='Windows'"
    ],
    # ...
)

参考资料:

【讨论】:

    猜你喜欢
    • 2013-11-03
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 2017-04-06
    相关资源
    最近更新 更多