5

After upgrading from Python 3.6 to 3.7 (Windows), what is the correct method to upgrade all existing packages installed with Pip in the previous version? This is not using virtualenv or pipenv.

MaxRussell
  • 518
  • 1
  • 11
  • 25
  • 2
    Edit: Clarification, updating the old libs isn't the same as moving them over to the new installation. https://stackoverflow.com/questions/8636386/python-moving-to-a-new-computer is a few good examples of how to move over your libs. Normally, you don't upgrade packages after a python installation, the old and the new version are two separate entities. This is a good resource for upgrade tho [Upgrading all packages with pip](https://stackoverflow.com/questions/2720014/upgrading-all-packages-with-pip) is your friend. If you want to move over all installed libs from your old version – Torxed Oct 16 '18 at 09:22
  • 2
    Possible duplicate of [Upgraded Python; Do I have to reinstall all site-packages manually?](https://stackoverflow.com/questions/45563909/upgraded-python-do-i-have-to-reinstall-all-site-packages-manually) – VPfB Oct 16 '18 at 09:25
  • 2
    I guess a good idea would be to `pip freeze > old_reqs.txt` from Python3.6 and then `pip install -r old_reqs.txt --upgrade` in Python3.7. – Wiggy A. Oct 16 '18 at 09:32
  • Does this answer your question? [How to move all modules to new version of Python (from 3.6 to 3.7)](https://stackoverflow.com/questions/51308683/how-to-move-all-modules-to-new-version-of-python-from-3-6-to-3-7) – mkrieger1 Feb 10 '22 at 16:49

3 Answers3

3

You can try the following script to upgrade all the installed packages.

pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip install -U
Girish Vas
  • 680
  • 1
  • 10
  • 22
  • [Source](https://stackoverflow.com/a/3452888/1386750). Note that this just upgrades all installed packages for the current Python version. If you just upgraded Python, there will be zero packages. – AstroFloyd Oct 22 '20 at 15:09
1

You can upgrade all outdated packages directly:

pip install -U $(pip list -o freeze | cut -f1 -d=)

Long version:

pip install --upgrade $(pip list --outdated --format freeze | cut --fields=1 --delimiter="=")

Or you can create and use a file to list all outdated pip packages names:

list all outdated pip packages and format the output as "freeze";

-d= cut everything after "=" (delimiter);

> dump the result to a file.

pip list -o freeze | cut -f1 -d= > pip_list_outdated.txt

Long version:

pip list --outdated --format freeze | cut --fields=1 --delimiter="="> pip_list_outdated.txt

The output will be something like:

gunicorn
PySimpleGUI
python-engineio
python-socketio
requests
setuptools
six

Upgrade to latest version outdated pip packages using the name in each line:

pip install -U $(<pip_list_outdated.txt)

Long version:

pip install --upgrade $(<pip_list_outdated.txt)

Wrong way:

If you type:

pip list -o freeze:

You will get something like:

autopep8==1.4.3
chardet==3.0.4
Django==2.1.4

And if you try to upgrade using this result:

pip install -U $(pip list -o freeze)

You will get the messages:

Requirement already up-to-date: autopep8==1.4.3 in ...
Requirement already up-to-date: chardet==3.0.4 in ...
Requirement already up-to-date: Django==2.1.4 in ...

It happens because the version listed in the result is already installed.

To upgrade to the latest version, you need the package name without the version or the name with the version number you want to upgrade.

Treedbox
  • 690
  • 7
  • 8
1

I used a variation of upgrading all pip packages without Python upgrade using two different versions of pip (and for my user packages):

pip3.6 list --user --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip3.7 install --user --upgrade

pip3.6 will list the packages that are installed for Python 3.6, and pip3.7 will install the packages from that list for Python 3.7. Leave out the --user flag (twice) if you don't have user packages.

AstroFloyd
  • 405
  • 5
  • 14