18

How do I pip install the latest patch number version of a package within a major-minor release. So let's say I want the latest patch release of 1.10 so if there's 1.10.8, 1.10.9, 1.11.3 available, I want to get 1.10.9.

clarification: I don't want to install a specific package, I want to install the latest package within a range. Above I want the latest package within the 1.10.0 <= x < 1.11.0 range

Jad S
  • 2,705
  • 6
  • 29
  • 49

4 Answers4

35

pip supports the ~= version specifier for specifying the version number, so

pip install package~=1.10.0

would install version 1.10.9 if that is the latest patch level of that package.

There are more detailed explanations and comparisons to other methods of achieving the same results in the docs, for example:

~= 2.2.0

is equivalent to

>= 2.2.0, == 2.2.*
Nick Chammas
  • 11,843
  • 8
  • 56
  • 115
  • 4
    This does not update the package if you already have a compatible version installed. For example, if you already have "package" version 1.10.4 installed, then `pip install package~=1.10.0` will give a `requirement already satisfied ...` (using pip 18.0). – djvg Sep 13 '18 at 09:30
  • @djvg you need a `pip install -U package~=1.10.0` to update a package already installed – Alberto Chiusole Oct 27 '22 at 03:30
  • @AlbertoChiusole Yes. `-U` or `--upgrade`: ["Upgrade all specified packages to the newest available version."](https://pip.pypa.io/en/stable/cli/pip_install/#cmdoption-U), as mentioned in my [answer](https://stackoverflow.com/a/52311590) elsewhere on this page. – djvg Oct 27 '22 at 07:38
11

If you already have a compatible version of package installed, the accepted answer by user3850 will not upgrade to the latest patch (in my experience, that is).

For example I have django 1.9.8 installed and want to upgrade to the latest patch, which is 1.9.13, but pip install django~=1.9.0 (or pip install django~=1.9) tells me requirement already satisfied (using pip 18.0).

So, in this case we need to add --upgrade. There are two options that work for me:

  1. pip install django~=1.9.0 --upgrade

and one that I find more readable (based on this answer):

  1. pip install django==1.9.* --upgrade

If using the first option (~=) make sure to include the "micro" version number (as in "major.minor.micro"). For example, pip install django~=1.9.0 --upgrade upgrades to 1.9.13, as desired, but pip install django~=1.9 --upgrade (i.e. without the .0) upgrades to 1.11.15 instead.

Note: the lack of a lower bound, e.g. =>1.9.8, in option 2. should not be an issue because upgrade would give us the latest match anyway.

djvg
  • 11,722
  • 5
  • 72
  • 103
1

Try out the following:

pip install 'SomeProject>=1,<2'

Example:

pip install 'some_package>=1.10.0,<1.11.0'

Check out the pip documentation for more detailed explanantions.

Parth Sharma
  • 95
  • 2
  • 6
  • you even linked to the very docs where a better method is shown. why not use that instead? –  Apr 16 '18 at 10:38
-1

To install a package with specific version

pip install <package_name> == <version number>

For example

pip install openpyxl==2.6.0