【问题标题】:Install python dependency only when platform is Windows仅当平台为 Windows 时才安装 python 依赖项
【发布时间】:2019-09-14 08:44:48
【问题描述】:

我想在platform_system == Windows 时将pywin32 作为条件Python 依赖项 添加到setup.py

谁能给我一个关于如何使它工作的提示?

在探索了stackoverflow之后,还没有找到python2.7的答案。

我正在使用 Python 2.7、setuptools 28.x.x、pip 19.x.x。 Egg-info 是自动构建的。

from setuptools import setup, find_packages
import platform

platform_system = platform.system()

setup(
    name=xxx,
    version=xxx,
    packages=find_packages(),
    include_package_data=True,
    install_requires=[
        'matplotlib',
    ],
    extras_require={
        'platform_system=="Windows"': [
            'pywin32'
        ]
    },
    entry_points='''
        [console_scripts]
        xx:xx
    ''',
)

我不明白extras_require 中的键是如何工作的。 platform_system会参考前面platform_system的定义吗?

我也试过了:

from setuptools import setup, find_packages
import platform

setup(
    xxx
    install_requires=[
        'matplotlib',
        'pywin32;platform_system=="Windows"',
    ],
)

但这仅适用于python_version>=3.4

另外,https://www.python.org/dev/peps/pep-0508/ 似乎不适合我。

【问题讨论】:

    标签: python-2.7 dependencies setuptools


    【解决方案1】:

    检查 Python os 模块

    os.name 导入的操作系统相关模块的名称。目前已注册以下名称:'posix'、'nt'、'os2'、'ce'、'java'、'riscos'。

    nt 用于 Windows 操作系统。

    import os
    
    if os.name == 'nt':
        # Windows-specific code here...
    

    您也可以查看sys.platform

    sys.platform 例如,此字符串包含一个平台标识符,可用于将特定于平台的组件附加到 sys.path。

    import sys
    
    if sys.platform.startswith('win32'):
        # Windows-specific code here...
    
    

    已编辑: 根据您的问题,如果操作系统是 Windows,您想安装 pywin32。 我认为这段代码会对你有所帮助:

    import sys
    
    
    INSTALL_REQUIRES = [
        # ...
    ]
    EXTRAS_REQUIRE = {
        # ...
    }
    
    if sys.platform.startswith('win32'):
        INSTALL_REQUIRES.append("pywin32")
    
    setup(
        # ...
        install_requires=INSTALL_REQUIRES,
        extras_require=EXTRAS_REQUIRE,
    )
    

    【讨论】:

    • if 语句是否适用于设置?你能给我一个设置示例吗?
    • 如果这是真的,请检查我的回答。 @shinyshinx
    猜你喜欢
    • 2014-10-02
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 2012-05-19
    • 2014-10-31
    • 1970-01-01
    • 2012-09-17
    相关资源
    最近更新 更多