默认安装 "optional" 依赖项 Python (setuptools)
Install by default, "optional" dependencies in Python (setuptools)
有没有办法为 Python 包指定可选的依赖项,默认情况下应该 从 pip
安装 但不应该安装不能安装算不算失败?
我知道我可以指定 install_requires
以便为 90% 使用可以轻松安装某些可选依赖项的操作系统的用户安装软件包,我也知道我可以指定 extra_require
指定用户可以声明他们想要完整安装以获得这些功能,但我还没有找到一种方法来进行默认安装 pip
尝试安装软件包,但如果无法安装则不要抱怨。
(我想更新 setuptools
和 setup.py
的特定包称为 music21
95% 的工具可以 运行 没有matplotlib、IPython、scipy、pygame、一些鲜为人知的音频工具等,但如果安装了这些软件包,该软件包将获得额外的能力和速度,我更愿意让人们拥有这些默认能力,但如果无法安装则不报错)
无论如何都不是完美的解决方案,但您可以设置一个 post-install 脚本来尝试安装软件包,如下所示:
from distutils.core import setup
from distutils import debug
from setuptools.command.install import install
class PostInstallExtrasInstaller(install):
extras_install_by_default = ['matplotlib', 'nothing']
@classmethod
def pip_main(cls, *args, **kwargs):
def pip_main(*args, **kwargs):
raise Exception('No pip module found')
try:
from pip import main as pip_main
except ImportError:
from pip._internal import main as pip_main
ret = pip_main(*args, **kwargs)
if ret:
raise Exception(f'Exitcode {ret}')
return ret
def run(self):
for extra in self.extras_install_by_default:
try:
self.pip_main(['install', extra])
except Exception as E:
print(f'Optional package {extra} not installed: {E}')
else:
print(f"Optional package {extra} installed")
return install.run(self)
setup(
name='python-package-ignore-extra-dep-failures',
version='0.1dev',
packages=['somewhat',],
license='Creative Commons Attribution-Noncommercial-Share Alike license',
install_requires=['requests',],
extras_require={
'extras': PostInstallExtrasInstaller.extras_install_by_default,
},
cmdclass={
'install': PostInstallExtrasInstaller,
},
)
执行此操作的最简单方法是添加一个自定义安装命令,该命令只需将 shell 输出到 pip 即可安装 "optional" 包。在你的 setup.py
:
import sys
import subprocess
from setuptools import setup
from setuptools.command.install import install
class MyInstall(install):
def run(self):
subprocess.call([sys.executable, "-m", "pip", "install", "whatever"])
install.run(self)
setup(
...
cmdclass={
'install': MyInstall,
},
)
如上文 hoefling
所述,如果您发布源代码分发(.tar.gz
或 .zip
),这 仅 有效。如果您将包发布为内置发行版 (.whl
),它将不起作用。
这可能就是您要找的。它似乎是安装工具的内置功能,允许您声明“可选依赖项”。
https://setuptools.pypa.io/en/latest/userguide/dependency_management.html#optional-dependencies
例
setup(
name="Project-A",
...
extras_require={
'PDF': ["ReportLab>=1.2", "RXP"],
'reST': ["docutils>=0.3"],
}
)
有没有办法为 Python 包指定可选的依赖项,默认情况下应该 从 pip
安装 但不应该安装不能安装算不算失败?
我知道我可以指定 install_requires
以便为 90% 使用可以轻松安装某些可选依赖项的操作系统的用户安装软件包,我也知道我可以指定 extra_require
指定用户可以声明他们想要完整安装以获得这些功能,但我还没有找到一种方法来进行默认安装 pip
尝试安装软件包,但如果无法安装则不要抱怨。
(我想更新 setuptools
和 setup.py
的特定包称为 music21
95% 的工具可以 运行 没有matplotlib、IPython、scipy、pygame、一些鲜为人知的音频工具等,但如果安装了这些软件包,该软件包将获得额外的能力和速度,我更愿意让人们拥有这些默认能力,但如果无法安装则不报错)
无论如何都不是完美的解决方案,但您可以设置一个 post-install 脚本来尝试安装软件包,如下所示:
from distutils.core import setup
from distutils import debug
from setuptools.command.install import install
class PostInstallExtrasInstaller(install):
extras_install_by_default = ['matplotlib', 'nothing']
@classmethod
def pip_main(cls, *args, **kwargs):
def pip_main(*args, **kwargs):
raise Exception('No pip module found')
try:
from pip import main as pip_main
except ImportError:
from pip._internal import main as pip_main
ret = pip_main(*args, **kwargs)
if ret:
raise Exception(f'Exitcode {ret}')
return ret
def run(self):
for extra in self.extras_install_by_default:
try:
self.pip_main(['install', extra])
except Exception as E:
print(f'Optional package {extra} not installed: {E}')
else:
print(f"Optional package {extra} installed")
return install.run(self)
setup(
name='python-package-ignore-extra-dep-failures',
version='0.1dev',
packages=['somewhat',],
license='Creative Commons Attribution-Noncommercial-Share Alike license',
install_requires=['requests',],
extras_require={
'extras': PostInstallExtrasInstaller.extras_install_by_default,
},
cmdclass={
'install': PostInstallExtrasInstaller,
},
)
执行此操作的最简单方法是添加一个自定义安装命令,该命令只需将 shell 输出到 pip 即可安装 "optional" 包。在你的 setup.py
:
import sys
import subprocess
from setuptools import setup
from setuptools.command.install import install
class MyInstall(install):
def run(self):
subprocess.call([sys.executable, "-m", "pip", "install", "whatever"])
install.run(self)
setup(
...
cmdclass={
'install': MyInstall,
},
)
如上文 hoefling
所述,如果您发布源代码分发(.tar.gz
或 .zip
),这 仅 有效。如果您将包发布为内置发行版 (.whl
),它将不起作用。
这可能就是您要找的。它似乎是安装工具的内置功能,允许您声明“可选依赖项”。
https://setuptools.pypa.io/en/latest/userguide/dependency_management.html#optional-dependencies
例
setup(
name="Project-A",
...
extras_require={
'PDF': ["ReportLab>=1.2", "RXP"],
'reST': ["docutils>=0.3"],
}
)