什么决定了将使用哪个索引`pip`?

What determines which index `pip` is going to use?

我希望pip从我的操舵室安装轮子,并回退到 PyPI(通过缓存代理)当且仅当轮子从操舵室丢失时。

我试图通过调用

来实现这一点
pip install -U --index-url $PYPI_PROXY_URL --find-links $WHEELHOUSE_URL \
            -r requirements.txt

然而,尽管 wheelhouse 拥有所有必需的包,但它在获取包的位置并不是确定性的,而是在它们的来源、代理 PyPI 或 wheelhouse 上看起来相当随机。

我希望这是确定性的,并且总是首先选择驾驶室。如何使用 pip 实现该目标?

我知道 --no-index 会强制它只使用驾驶室,但我想保留回退驾驶室丢失的包裹的能力。

深入研究 pip 的源代码我发现:

  1. 使用内部 _candidate_sort_key 函数对有效候选人进行排序,其工作方式如下:

        If not finding wheels, then sorted by version only.
        If finding wheels, then the sort order is by version, then:
          1. existing installs
          2. wheels ordered via Wheel.support_index_min(self.valid_tags)
          3. source archives
        Note: it was considered to embed this logic into the Link
              comparison operators, but then different sdist links
              with the same version, would have to be considered equal
    
  2. 在其他条件相同的情况下,它回落到 hardcoded 顺序,即:

    1. 本地文件系统
    2. 索引网址
    3. 查找链接 URL
    4. 依赖链接 URL

从 pip 9.0.1 开始,上述顺序是硬编码的,因此无法使用设置或参数更改它。