17

In Python 2 I can install a set of packages via pip preferring binary packages over source packages (meaning: fallback to source if binary not found) with:

(1) pip install --prefer-binary -r requirements.txt

In Python 3 I can do this with:

(2) pip3 install --only-binary=:all: -r requirements.txt

But (1) is not exactly equal to (2) since the former says:

Prefer binaries when installing; but if I don't find a binary option, then I'll go with source.

The latter says:

I will fail if no binaries are found; don't even try from source.

So, from the docs it seems that one solution could be to just manually enter each package which should be considered for source installation - meaning: the "only-binary" flag can be provided multiple times on the command line and can thus handle special-cases like that (by emptying it out, or giving specific package names to the binary packages). This answer details, to some extent, that approach: Make pip download prefer to download source-distributions (not wheels).

However, I have a large number of both types of packages so I need an automated way like the (1) approach.

Question: How can I get a similar automated behavior as (1) but in Python/pip 3?

Solution: Pip is not Python - upgrade pip to vs. 20.X and use --prefer-binary.

Jesper - jtk.eth
  • 7,026
  • 11
  • 36
  • 63
  • 1
    Actually, maybe this is just due to an outdated pip? If I upgrade to vs. 20 this might fix it? https://pip.pypa.io/en/stable/reference/pip_install/ – Jesper - jtk.eth May 11 '20 at 16:24
  • 6
    python is not pip. It is not fundamentally different when using python 2 or python3. `prefer-binary` is still part of the pip command line api, as you can see [here](https://pip.pypa.io/en/stable/reference/pip_install/#cmdoption-prefer-binary) – FlyingTeller May 11 '20 at 16:37
  • Thanks @FlyingTeller - I updated my answer with the solution too. I had connected the two too tight in my head - this was very helpful. – Jesper - jtk.eth May 11 '20 at 17:29
  • 1
    You can answer your own question. Please add `upgrade pip to vs. 20.X and use --prefer-binary` as an answer – cowlinator May 19 '21 at 01:07

2 Answers2

11

Solution: upgrade pip to vs. 20.X and use --prefer-binary

Jesper - jtk.eth
  • 7,026
  • 11
  • 36
  • 63
5

now its feasible to configure that on requirements.txt

--prefer-binary

<packages>
....

https://github.com/pypa/pip/pull/7996

Oshan Wisumperuma
  • 1,808
  • 1
  • 18
  • 32