2

I have a library that has to support several Python versions starting from 2.6. E.g., one of my requirements is requests. I know that the last version supporting Python 2.6 is 2.19.1 (the latest version 2.20.0 supports any Python >= 2.7).

So my question is whether pip understands that the line requests~=2.19 in requirements should install 2.19.1 for Python 2.6 and 2.20.0 for any higher version of Python?

Update. The question is not about the syntax, I'm curious whether pip is smart enough and in a Python 2.6 environment will be able not to go beyond 0.19.1.

Gevorg Davoian
  • 484
  • 5
  • 13
  • might help https://stackoverflow.com/a/39590286/10262890 – Manali Kagathara Feb 17 '20 at 11:46
  • `requests<=2.19.1 ; python_version<'2.7'` – sinoroc Feb 17 '20 at 12:07
  • 1
    Probably not a direct answer to your (updated) question, but some food for thought maybe... The issue with relying on the fact that _pip_ is "_smart enough_", is that _pip_ relies on the project's metadata, and this could be faulty. There could be a new version _2.19.3_ (or _2.21.999_, or whatever in _2.*_) that wrongly advertises itself as compatible with _Python 2.6_ (assume a mistake, not necessarily bad intention), and _pip_ would install it. So it's probably better to be as explicit as possible. But from looking at the specifications, it seems like `requests~=2.19` would do what you need. – sinoroc Feb 17 '20 at 12:57

2 Answers2

1

I believe yes, pip does take into account the version of the currently running Python interpreter, to choose an appropriate version (or distribution of a version) of a project to install. So since pip install 'requests~=2.19' is the equivalent of pip install 'requests>=2.19,requests==2.*', it should give as of today (2020-02-18):

  • requests 2.19.1 on Python 2.6; and
  • requests 2.22.0 on Python 2.7.
sinoroc
  • 18,409
  • 2
  • 39
  • 70
0

As far as I know, yes that is possible using version specifiers in your requirements file.

Example:

requests>=2.19.1,<=2.20.0

Using the ~= operator would result in a package in the 2.19 package range for Python 2.7, because it means: install a package greater than or equal to version 2.19.1, but still in the 2.19 version.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
gummy
  • 123
  • 1
  • 6
  • Syntax seems wrong to me (probably a comma missing somewhere) and this doesn't answer the question about the `~=` operator. – sinoroc Feb 17 '20 at 11:12
  • i'm sorry, I forgot the comma. I updated my answer. – gummy Feb 17 '20 at 11:19
  • 1
    Hm, according to the [official documentation](https://pip.pypa.io/en/stable/reference/pip_install/#example-requirements-file), `requests~=2.19` means `requests>=2.19,requests==2.*`, doesn't it? – Gevorg Davoian Feb 17 '20 at 11:40
  • 1
    Yes, you're right. There is one thing you need to keep in mind which is it will also take newer versions like 2.3, 2.4 etc, which might cause incompatibility issues later on. – gummy Feb 17 '20 at 13:49