【问题标题】:setuptools and pip: choice of minimal and complete installsetuptools 和 pip:最小和完整安装的选择
【发布时间】:2023-03-27 20:03:01
【问题描述】:

我们创建了一个依赖于其他库的库。但是有必要的(例如用于服务器批处理)和可选的依赖项(例如用于具有 GUI 的客户端)。

这样的事情可能吗:

pip install mylib.tar.gz  # automatically downloads and installs with the minimal set of dependencies

pip install mylib.tar.gz  --install-option="complete"  # automatically installs with all dependencies

我找到了extra_require 标志,但我如何告诉pip 使用它们? setup.py 看起来像这样:

from setuptools import setup

# ...

# Hard library depencencies:
requires = [
    "numpy>=1.4.1",
    "scipy>=0.7.2",
    "traits>=3.4.0"
]

# Soft library dependencies:
recommended = {
    "mpl": ["matplotlib>=0.99.3"],
    "bn": ["bottleneck>=0.6"]
}

# ...

# Installer parameters:
setup(
    name = "mylib",
    #...
    install_requires = requires,
    extras_require = recommended
)

【问题讨论】:

标签: python pip setuptools


【解决方案1】:

所以 pip 实际上对安装有额外要求的库非常挑剔

pip install -e ".[extra,requirements]"    # works with file paths
pip install "package[extra,requirements]" # works when downloading packages
pip install ".[extra,requirments]"        # DOES NOT WORK

我认为这取决于RequirementsSpec 解析器的工作方式,并且pip 使用-e 标志做了一些额外的魔法。无论如何,经过多次头部撞击,这是一个有点丑陋的解决方法

pip install "file:///path/to/your/python_code#egg=SomeName[extra,requirements]"

egg=SomeName 部分基本上被忽略了,但 pip 正确地提取了额外的要求

注意事项

  • 已使用 pip 1.5.6 进行测试,因此请确保您使用的是当前版本的 pip。
  • 据我所知,file:/// 语法在 pip 中没有记录,所以我不确定它是否会在未来发生变化。它看起来有点像 VCS Support 语法,但我有点惊讶它的工作原理。
  • 您也可以通过运行自己的 pypi 服务器来解决此问题,但这有点超出范围。

【讨论】:

    【解决方案2】:

    您可以安装extras_require 中的软件包,方法是在 pip 中的软件包名称后面附加方括号中推荐的依赖项名称(即[mpl][bn])。

    所以要安装带有附加要求的“mylib”,你可以这样调用 pip:

    pip install 'mylib[mpl]'
    pip install 'mylib[bn]'
    

    这将首先下载并安装额外的依赖项,然后是mylib的核心依赖项。

    这与您使用 setuptools 声明这些依赖项的方式类似:http://pythonhosted.org/setuptools/setuptools.html#declaring-extras-optional-features-with-their-own-dependencies(参见第三个示例中的 install_requires 值)

    【讨论】:

    • 啊好吧!这也适用于更多? pip install 'mylib[mpl,bn]' 或语法是什么?当将"complete": ["matplotlib>=0.99.3", "bottleneck>=0.6"] 添加到recommended 字典时,所有库都可能安装有pip install 'mylib[complete]' 之类的东西?
    • 我不知道它是否适用于多个额外的依赖项,我只对单个依赖项使用过它...但是您的第二个解决方案应该可以工作,最好是这样:extras_require = dict(('complete', itertools.chain.from_iterable(recommended)) + recommended)。这样,您不必在 setup.py 中两次声明这些依赖项,而是可以重用来自 recommended 的值。
    • 是的,我认为它只适用于包名。您当然可以将 tgz 解压缩到一个临时文件夹并通过 pip install .[extras] 安装它
    猜你喜欢
    • 1970-01-01
    • 2013-06-13
    • 2013-08-23
    • 2018-07-08
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    相关资源
    最近更新 更多