2

Let us consider that we have installed python-pip and python-pip3 packages to get pip and pip3 on our system.

As far I can understand pips are package managers for pypi.org. I expect that they have functionality similar to apt update, apt upgrade and apt dist-upgrade.

How to correctly upgrade all python modules to newest stable versions with pip or pip3?

N0rbert
  • 99,918

4 Answers4

2

I've tested this on pip2 and pip3 with success although I'm not sure if there's a better way to do it.

pip freeze | sed 's/==.*//' | xargs pip install -U

You may like to add --user after pip freeze and pip install if you only want to upgrade packages installed to your user account instead of the system. If you're upgrading system Python packages you'll need to run the pip install with sudo otherwise you'll likely hit a permission denied error.

1

This Could work for older versions of pip:

pip freeze --local | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip install -U
1

With pip there isn't a concept of 'newest stable' as there is in Debian/Ubuntu.

The 'newest stable' packages in Debian/Ubuntu are tested to work correctly with all the other packages in the distribution.

With pip, you can upgrade all the python modules, but you will upgrade all the modules to the latest version available in the repository. It will be your responsibility to verify that everything still continues to work. There may be issues due to new bugs or incompatible changes.

Because of that never upgrade the OS provided python modules with pip (option: --system) unless you're ready to repair the possible breakage.

It can make a lot of sense to upgrade all the modules installed in your user environment, or better inside a virtual environment. The command should be:

pip freeze | awk '{print $1}' | xargs pip install -U

For users of different distributions and other cases, check:

pip help install

since the default behavior (no --user or --system options) changes from Debian derivatives to standard python.

Telegrapher
  • 2,827
  • To avoid upgrading the system installed packages, you can use pip freeze --user or pip freeze -U. Otherwise, it will list all packages, including system packages like ubuntu-drivers-common and unattended-upgrades and others. – mchid Jan 19 '22 at 09:41
1

Here's what I do to upgrade the pip packages installed locally (to the user's $HOME directory) without the use of sudo:

pip install --upgrade --user $(pip list --user | awk '{print $1}' | sed '1,2d')

This avoids manually upgrading packages like Wheel and other system packages, which can break your setup.

mchid
  • 43,546
  • 8
  • 97
  • 150