Search code examples
pythonpip

pip specify package version either '==x.y' or '>=a.b'


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

Solution

  • 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.