繁体   English   中英

setuptools 和 pip:选择最小安装和完整安装

[英]setuptools and pip: choice of minimal and complete install

我们创建了一个依赖于其他库的库。 但是有必要的(例如对于服务器批处理)和可选的依赖项(例如对于具有 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
)

您可以通过在extras_require的包名称后附加方括号中推荐的依赖项的名称(即[mpl][bn]在您的情况下)来安装extras_require中的包。

因此,要安装具有附加要求的“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 实际上对安装有额外要求的库非常挑剔

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 支持语法,但我有点惊讶它的工作原理。
  • 您也可以通过运行自己的 pypi 服务器来解决这个问题,但这有点超出范围。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM