2

When installing with pip -- or by specifying through requirements.txt -- how do I specify that the version is either:

  • ==x.y, or
  • >=a.b

where x.y < a.b.

For example, I want a package to be either ==5.4 or >=6.1.

Let's say I need to do this because:

  • I want my program to run on Python>=3.7
  • The last package supported for Python 3.7 is "5.4"
  • For Python>3.7, the latest package is "6.1.*"
  • I am avoiding "6.0.*" because of a slight incompatibility, which had been fixed in "6.1.*", and I want pip to not spend any time trying to check the "6.0.*" line
pepoluan
  • 6,132
  • 4
  • 46
  • 76

2 Answers2

2

Simply put the below line in your requirements.txt file -

some_package != 6.0.*

What will actually above line do?

The answer is, when pip install -r requirements.txt is executed it will try to find the latest version except 6.0.* . Suppose, if the latest version is 6.0.7 then it will skip this version and install earlier version like 5.9.12. On the other hand, if the latest version is 6.1.6 then it will install the latest version.

One more thing. If you wish, you can also specify the python version in requirements.txt. Pip will install package according to your project's python version.

pkg1 != 6.0.* ; python_version >= "3.7"
pkg1 < 5.4    ; python_version < "3.7"

Adding multiple lines for same package with python_version specification will install package version according to your project's python version.

  • 1
    Ahh good idea. Didn't realize I can actually specify same package more than once in `requirements.txt` as long as their specification lines are different. Thanks! – pepoluan Dec 23 '22 at 09:52
-1
pip install package>=5.4.*,<=6.1.*,!=6.0.*
  • Did you mean `>=6.1`? – user2357112 Dec 22 '22 at 06:20
  • No, on second thought, you probably didn't, or you wouldn't have put `!=6.0`. What you've suggested doesn't match what the questioner asked for, though; they wanted either 5.4, or a version that is at least 6.1. – user2357112 Dec 22 '22 at 06:21
  • No I meant <=6.1 as there is no version above 6.1 so it is meaningless to write >=6.1 further more writing !=6.0.* reduces any chances of installing the non compatible version – aditya thakker Dec 23 '22 at 09:17
  • The questioner is exactly pointing to checking all the versions that are available to install and skip checking for the 6.0 one, now he wants a setup such that if somehow for some reason the package with version number 6.1.* is dropped then atleast the version latest before 6.1 should be installed excluding the 6.0.* version – aditya thakker Dec 23 '22 at 09:23
  • It is like asking you to go to several bakeries, one of then established since 50years,one of them completely new and one of them two months old, now you know the two months old isn't making good stuff and you wanna try the new bakery, somehow some days later you find that the new bakery has lost its taste so tha last resort will be the 50year old one where you would always find the good stuff – aditya thakker Dec 23 '22 at 09:27
  • The question says the latest version is "6.1.*", meaning there may be a 6.1.1 or higher. Your answer excludes 6.1.1 and up. It also excludes 6.2 once that comes out, contrary to what the questioner asked for. – user2357112 Dec 23 '22 at 09:35
  • Well obviously if you ask for latest version that has to be changed according to specificity – aditya thakker Dec 23 '22 at 10:57