简体   繁体   中英

Upgrading pip packages after python upgrade

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.

您可以尝试以下脚本来升级所有已安装的软件包。

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM