仅在平台为 Windows 时安装 python 依赖项
Install python dependency only when platform is Windows
我想在 platform_system == Windows
时将 pywin32
添加为 条件 Python 依赖项 到 setup.py
谁能告诉我如何让它发挥作用?
查了Whosebug,还没找到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/ 似乎对我不起作用。
检查Pythonos模块
os.name
导入的操作系统相关模块的名称。当前已注册以下名称:'posix'、'nt'、'os2'、'ce'、'java'、'riscos'.
和 nt 用于 windows OS.
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...
已编辑:
根据你的问题,如果 OS 是 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,
)
您可以在依赖项的规范中使用环境标记。
参见 PEP 508。
为了检测操作系统是否为Windows,可以使用os_name
等于nt
。
根据显示 "name; os_name=='a' or os_name=='b'"
的 PEP 中的语法,您的 install_requires 部分可以是:
install_requires=[
"matplotlib",
"pywin32; os_name=='nt'",
],
不需要为此使用 extras_require
。
我想在 platform_system == Windows
pywin32
添加为 条件 Python 依赖项 到 setup.py
谁能告诉我如何让它发挥作用?
查了Whosebug,还没找到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/ 似乎对我不起作用。
检查Pythonos模块
os.name
导入的操作系统相关模块的名称。当前已注册以下名称:'posix'、'nt'、'os2'、'ce'、'java'、'riscos'.
和 nt 用于 windows OS.
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...
已编辑: 根据你的问题,如果 OS 是 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,
)
您可以在依赖项的规范中使用环境标记。 参见 PEP 508。
为了检测操作系统是否为Windows,可以使用os_name
等于nt
。
根据显示 "name; os_name=='a' or os_name=='b'"
的 PEP 中的语法,您的 install_requires 部分可以是:
install_requires=[
"matplotlib",
"pywin32; os_name=='nt'",
],
不需要为此使用 extras_require
。