Python tip: How to batch update installed libraries?

As we all know, to upgrade a library (assuming xxx), you can use the pip install --upgrade xxxcommand, or abbreviated as pip install -U xxx.

If there are multiple libraries, they can be written in sequence after xxx, separated by spaces. So, how to batch update all the installed libraries in the system in a simple and elegant way?

Next, let's go straight to the topic and take you to learn several methods/saucy operations!

Method 1: pip list combined with Linux commands

pip listThe command can query the installed libraries, and combined with some Linux commands (cut, sed, awk, grep...), batch upgrades can be implemented directly on the command line.

First query to see what format it is:

As you can see, the first two lines are some prompt information, we need to filter from the third line, then we can use the awkcommand:

python3 -m pip list | awk 'NR>=3{print}' | awk '{print $1}' | xargs python3 -m pip install -U

Explain the operation process of this command: first list query, then the first awk fetches the content of the line number greater than or equal to 3, the second awk fetches the content of the first column, and then passes it as a parameter to the final upgrade command.

(PS: There are different versions of Python on the test server, so they are specified. Regarding the usage of "-m", it is recommended to read: Typical usage, principle analysis and development of -m in Python )

pip also supports querying outdated repositories, i.e. using the pip list --outdatedcommand. By default, the format of the query is pip listsimilar to , and the valid content starts from the third line. You can try it.

In addition, we can also specify --format=freezethe format, the effect is as follows:

In this format, you can use the cutcommand to cut the "=" sign, and then take the first column:

pip list --outdated --format=freeze | cut -d = -f 1 | xargs pip install -U

The above command does not work in Windows system. Is there a more general method?

Method 2: Use pip freeze

If you are upgrading the installed library in full, you can first pip freezegenerate the dependency file with the command to obtain the installed library and its current version number:

pip freeze > requirements.txt

Then modify the "==" in the file to ">=", and then execute:

pip install -r requirements.txt --upgrade

This method is more suitable for specific projects with dependency files, and the required libraries can be upgraded for the project.

Method 3: The method of calling pip in the code

Earlier pip libraries (<10.0.1) provided the get_installed_distributions() method to query installed libraries, which can be used in code:

# 只在早期 pip 版本中用
import pip
from subprocess import call

packages = [dist.project_name for dist in pip.get_installed_distributions()]
call("pip install --upgrade " + ' '.join(packages), shell=True)

In newer versions, this method has been deprecated, and the same function should be written like this:

# 较新的 pip 版本。但不建议使用
from subprocess import call
from pip._internal.utils.misc import get_installed_distributions

for dist in get_installed_distributions():
    call("pip install --upgrade " + dist.project_name, shell=True)

However, "_internal" is prefixed with an underscore, indicating that it is not intended to be used by the export.

Method 4: Use the pkg_resources library

There is another method similar to Method 2 and Method 3.

pkg_resourcesis part of the setuptoolslibrary used to find and manage Python libraries, version dependencies, associated resource files, and more. It can be written like this:

# 需要安装 setuptools
import pkg_resources
from subprocess import call

packages = [dist.project_name for dist in pkg_resources.working_set]
call("pip install --upgrade " + ' '.join(packages), shell=True)

Method 5: Use the pip-review library

pip-reviewLibraries is a tool specifically designed to facilitate upgrading Python libraries. You can view outdated libraries, automatically upgrade, or interactively selectively upgrade:

There is also a similar pip-upgraderlibrary, which is also to solve the problem of batch upgrade. Interested students should search by themselves.

Method 6: Full upgrade command of pip plan

The pip official has plans to provide a full upgrade (upgrade-all) command. If it is developed, it should be the best choice.

Then, the bad news is that this plan has been blocked for nearly three years, and the issue is still in the Open state, and I don't know when it will make progress. Let me mention it here for the time being, and pay attention in the future.

There are six methods introduced above, and each has its own applicable scenarios. Have you all learned it?

In addition, of course, there are other methods. For example, there is a " How to upgrade all Python packages with pip? " question on the stackoverflow website , which has more answers.

Thanks for reading, if you like this article, please search and follow "Python cat" to read more exciting content! https://mp.weixin.qq.com/s/yOMC1cxcmMDUxYyeB-dtpw

{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324040763&siteId=291194637