python - 默认情况下,在 Python 中安装“可选”依赖项(setuptools)
问题描述
有没有办法为 Python 包指定可选依赖项,默认情况下应该从中安装,但pip
如果无法安装,安装不应被视为失败?
我知道我可以指定install_requires
以便为 90% 使用可以轻松安装某些可选依赖项的操作系统的用户安装软件包,而且我也知道我可以指定extra_require
指定用户可以声明他们想要完整安装来获得这些功能,但我还没有找到一种方法来进行默认pip
安装尝试安装软件包,但如果无法安装它们也不会抱怨。
(我想更新的特定包setuptools
被setup.py
称为music21
95% 的工具可以在没有 matplotlib、IPython、scipy、pygame、一些晦涩的音频工具等的情况下运行,但如果这些包获得额外的能力和速度软件包已安装,我宁愿让人们默认拥有这些能力,但如果无法安装则不报告错误)
解决方案
无论如何都不是一个完美的解决方案,但您可以设置一个安装后脚本来尝试安装软件包,如下所示:
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,
},
)
推荐阅读
- php - 在 PHP 或 MySQL 中计算一个范围内的最大重叠事件
- c++ - shaderc IncluderInterface,包含失败?
- swift - 混合多个 MTLTexture
- .net - 相对于 VB.NET 墨卡托投影上的坐标的 X、Y 位置不正确
- reactjs - 火狐中的 NS_ERROR_NET_RESET
- javascript - 在 ForEach 等待每个元素的执行之间,如何?
- post - Stripe 返回的无效整数(UNIX 时间戳)
- java - Java和C#中匿名类的区别
- python - PyCharm 中的熊猫:意外的类型:(str) 可能的类型:(_SupportsIndex) (slice)
- docker - Kafka Connect 自动接收器/源初始化