默认安装,Python中的"可选"依赖项(setuptools)

Mic*_*ert 8 python pip setuptools music21

是否有指定这应该是一个Python包可选依赖的方式被默认安装的pip,但对于其安装不应该被认为是失败,如果不能安装它们?

我知道我可以指定install_requires使用可以轻松安装某些可选依赖项的操作系统的90%用户安装软件包,我也知道我可以指定extra_require用户可以声明他们想要完整安装来获取这些软件包功能,但我还没有找到一种方法来进行默认pip安装尝试安装软件包,但如果无法安装则不会抱怨.

(我想要更新setuptools和setup.pyfor 的特定包被称为music2195%的工具可以在没有matplotlib,IPython,scipy,pygame,一些模糊的音频工具等的情况下运行但是如果这些包有额外的能力和速度安装了软件包,我宁愿让人们默认拥有这些功能,但如果无法安装则不报告错误)

min*_*leg 5

无论如何都不是一个完美的解决方案,但你可以设置一个安装后的脚本来尝试安装软件包,如下所示:

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,
    },
)
Run Code Online (Sandbox Code Playgroud)

  • 请注意,安装后命令对于“pip”是没有用的,除非您明确指示用户通过“pip install pkgname --no-binary=pkgname”进行安装。这仅在直接调用“python setup.py install”时才有效。 (2认同)
  • 请不要建议用户导入`pip`:它是一个命令行工具,不支持任何可导入的API。这样做会导致用户崩溃和困惑,并且在需要在内部移动事物时让 `pip` 开发人员头疼(因此你的 `try`/`except` -- `pip._internal` 模块表明这是 `pip` 内部的) pip`,不应导入。) (2认同)