Search code examples
pythonpippython-wheel

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


I want pip to install wheel from my wheelhouse, and fallback to PyPI (via caching proxy) if and only if the wheel is missing from the wheelhouse.

I'm attempting to achieve this by calling

pip install -U --index-url $PYPI_PROXY_URL --find-links $WHEELHOUSE_URL \
            -r requirements.txt

However instead of being deterministic in where it gets the packages from it seems pretty random at where they're coming from, proxied PyPI or the wheelhouse, despite wheelhouse having all the required packages.

I want this to be deterministic and always choose the wheelhouse first. How can I achieve that with pip?

I know --no-index would force it to use only the wheelhouse, but I want to retain the ability to fallback for packages missing from the wheelhouse.


Solution

  • Digging into pip's source code I found out that:

    1. the valid candidates are sorted using internal _candidate_sort_key function, which works as following:

          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. All else being equal, it falls back to hardcoded order which is:

      1. local filesystem
      2. index URLs
      3. find-links URLs
      4. dependency-links URLs

    As of pip 9.0.1 the above order is hardcoded, so there is no way of changing it using settings or parameters.