是否可以使用pip一次性升级所有Python包?

注意:官方问题跟踪器上对此有一个功能请求。


当前回答

如果您安装了pip<22.3,则使用纯Bash/Z外壳一个衬垫实现:

for p in $(pip list -o --format freeze); do pip install -U ${p%%=*}; done

或者,以一种格式良好的方式:

for p in $(pip list -o --format freeze)
do
    pip install -U ${p%%=*}
done

在这之后,您将得到pip>=22.3,其中-o和--format冻结是互斥的,您不能再使用这一行。

其他回答

查看所有过时的软件包

 pip list --outdated --format=columns

安装

 sudo pip install pipdate

然后键入

 sudo -H pipdate

在Windows或Linux上更新Python包

1-将已安装软件包的列表输出到需求文件(requirements.txt)中:

pip freeze > requirements.txt

2-编辑requirements.txt,并将所有“==”替换为“>=”。在编辑器中使用“全部替换”命令。

3-升级所有过时的软件包

pip install -r requirements.txt --upgrade

资料来源:https://www.activestate.com/resources/quick-reads/how-to-update-all-python-packages/

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

这应该更有效:

pip3 list -o | grep -v -i warning | cut -f1 -d' ' | tr " " "\n" | awk '{if(NR>=3)print}' | cut -d' ' -f1 | xargs -n1 pip3 install -U

pip-list-o列出过时的包;grep-v-i warning反向匹配警告以避免更新时出错cut-f1-d1''返回第一个单词-过时包的名称;tr“\n|\r”“”将剪切的多行结果转换为单行、空格分隔的列表;awk“{if(NR>=3)print}”跳过标题行cut-d“”-f1获取第一列xargs-n1 pip install-U从管道左侧获取1个参数,并将其传递给命令以升级包列表。

通过拉取请求发送给pip人员;同时使用我写的这个pip库解决方案:

from pip import get_installed_distributions
from pip.commands import install

install_cmd = install.InstallCommand()

options, args = install_cmd.parse_args([package.project_name
                                        for package in
                                        get_installed_distributions()])

options.upgrade = True
install_cmd.run(options, args)  # Chuck this in a try/except and print as wanted