为什么 Pip 会忽略具有嵌套依赖项的已配置存储库?

Why does Pip disregard configured repository with nested dependencies?

问题

假设我在 Linux 机器上有一个完全空的 Python+Pip+R (pip 19.3.1) 环境,我想安装包 rpy2点子。因为我在公司防火墙后面,所以我将 pip 配置为使用私有存储库。

[global]
index-url = http://private.com/artifactory/api/pypi/PyPI/simple
trusted-host = private.com

现在我执行pip install rpy2,我会返回以下错误:

Couldn't find index page for 'cffi'
 Download error on https://pypi.python.org/simple/

因此 pip 尝试通过从官方 PyPi 存储库查找和安装 cffi 来解决嵌套依赖关系。它完全忽略了我配置的 repo。

当我 运行 pip install cffi && pip install rpy2 一个接一个地按预期工作时。

这是完整的错误输出:

ERROR: Command errored out with exit status 1:
 command: /usr/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-8vuadu93/rpy2/setup.py'"'"'; __file__='"'"'/tmp/pip-install-8vuadu93/rpy2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-8vuadu93/rpy2/pip-egg-info
     cwd: /tmp/pip-install-8vuadu93/rpy2/
Complete output (25 lines):
Download error on https://pypi.python.org/simple/cffi/: [Errno -2] Name or service not known -- Some packages may not be found!
Couldn't find index page for 'cffi' (maybe misspelled?)
Download error on https://pypi.python.org/simple/: [Errno -2] Name or service not known -- Some packages may not be found!
No local packages or working download links found for cffi>=1.13.1
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/tmp/pip-install-8vuadu93/rpy2/setup.py", line 183, in <module>
    'rinterface_lib/R_API_eventloop.h']}
  File "/usr/lib/python3/dist-packages/setuptools/__init__.py", line 128, in setup
    _install_setup_requires(attrs)
  File "/usr/lib/python3/dist-packages/setuptools/__init__.py", line 123, in _install_setup_requires
    dist.fetch_build_eggs(dist.setup_requires)
  File "/usr/lib/python3/dist-packages/setuptools/dist.py", line 513, in fetch_build_eggs
    replace_conflicting=True,
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 774, in resolve
    replace_conflicting=replace_conflicting
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 1057, in best_match
    return self.obtain(req, installer)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 1069, in obtain
    return installer(requirement)
  File "/usr/lib/python3/dist-packages/setuptools/dist.py", line 580, in fetch_build_egg
    return cmd.easy_install(req)
  File "/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 692, in easy_install
    raise DistutilsError(msg)
distutils.errors.DistutilsError: Could not find suitable distribution for Requirement.parse('cffi>=1.13.1')
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

问题

这是 Pip 中的错误,rpy2 的特定问题还是我遗漏了什么?

更新

运行 以下命令给我同样的错误:

pip install rpy2 --no-index --find-links http://private.com/artifactory/api/pypi/PyPI/simple
pip install rpy2 --index-url http://private.com/artifactory/api/pypi/PyPI/simple

我使用了 -vvv,问题似乎出现在 setuptools

的某个地方

解决方案

rpy2 使用 setuptools,后者再次使用 easy_install.py。它还适用于 index_url 变量。但它不是从 pip.config 而是从 distutils.cfg.

获取值

我确定了我用 find / -name "distutils" 安装的所有 Python 版本。然后我将具有以下内容的 distutils.cfg 添加到每个目录中:

[easy_install]
index_url = blablabla

现在可以了,我执行pip install rpy2,所有缺少的要求都一次性安装好

我认为这可能是因为 cffirpy2setup.py 中被列为 setup_requires。很可能是因为 cffi 需要 构建 项目本身,然后才能 安装 。这种 build 依赖没有被 pip 直接处理,所以它的 index-url 选项没有效果。

解决方案是告诉 setuptools 关于 distutils configuration file

中的替代索引
[easy_install]
index_url = https://my.index-mirror.com

参考文献: