如何使用 pip 升级所有 Python 包
Is it possible to upgrade all Python packages at one time with pip
?
Note: that there is a feature request for this on the official issue tracker.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
目前还没有内置标志。从 pip 版本 22.3 开始,
--outdated
和--format=freeze
已变为 相互排斥。使用 Python 解析 JSON 输出:如果您使用的是
pip<22.3
,您可以使用:对于旧版本的
pip
:grep 是跳过可编辑(“-e”)包定义,如 @jawache。 (是的,您可以将
grep
+cut
替换为sed
或awk
或perl
或...)。xargs
的-n1
标志可防止在更新某个包失败时停止所有操作(感谢 @andsens)。注意:这有无限的潜在变化。我试图让这个答案简短而简单,但请在评论中提出建议!
There isn't a built-in flag yet. Starting with pip version 22.3, the
--outdated
and--format=freeze
have become mutually exclusive. Use Python, to parse the JSON output:If you are using
pip<22.3
you can use:For older versions of
pip
:The
grep
is to skip editable ("-e") package definitions, as suggested by @jawache. (Yes, you could replacegrep
+cut
withsed
orawk
orperl
or...).The
-n1
flag forxargs
prevents stopping everything if updating one package fails (thanks @andsens).Note: there are infinite potential variations for this. I'm trying to keep this answer short and simple, but please do suggest variations in the comments!
要升级所有本地软件包,您可以安装
pip-review
:之后您可以以交互方式升级软件包:
或者自动升级:
pip-review
是pip-tools
的分支。请参阅pip-tools
问题 提到的="https://stackoverflow.com/questions/2720014/upgrading-all-packages-with-pip/16269635#comment51585726_16269635">@knedlsepp。pip-review
包可以工作,但pip-tools
包不再工作。pip-review
正在寻找新的维护者。pip-review
适用于 Windows 自版本 0.5。To upgrade all local packages, you can install
pip-review
:After that, you can either upgrade the packages interactively:
Or automatically:
pip-review
is a fork ofpip-tools
. Seepip-tools
issue mentioned by @knedlsepp.pip-review
package works butpip-tools
package no longer works.pip-review
is looking for a new maintainer.pip-review
works on Windows since version 0.5.您可以使用以下 Python 代码。与 pip freeze 不同,这不会打印警告和 FIXME 错误。
对于点< 10.0.1
对于点 >= 10.0.1
You can use the following Python code. Unlike
pip freeze
, this will not print warnings and FIXME errors.For pip < 10.0.1
For pip >= 10.0.1
以下内容适用于 Windows,也应该对其他人有好处(
$
是命令提示符中您所在的任何目录。例如,C:/Users/Username )。打开
文本文件,将
==
替换为>=
,或者让 sed 为您执行:并执行:
如果您遇到某个包停滞的问题升级(NumPy有时),只需进入目录($),注释掉名称(在其前面添加
#
)并再次运行升级。您可以稍后取消该部分的注释。这对于复制 Python 全局环境也非常有用。另一种方式:
我也喜欢 pip-review 方法:
您可以选择“a”来升级所有软件包;如果一次升级失败,请再次运行,然后继续进行下一次升级。
The following works on Windows and should be good for others too (
$
is whatever directory you're in, in the command prompt. For example, C:/Users/Username).Do
Open the text file, replace the
==
with>=
, or have sed do it for you:and execute:
If you have a problem with a certain package stalling the upgrade (NumPy sometimes), just go to the directory ($), comment out the name (add a
#
before it) and run the upgrade again. You can later uncomment that section back. This is also great for copying Python global environments.Another way:
I also like the pip-review method:
You can select 'a' to upgrade all packages; if one upgrade fails, run it again and it continues at the next one.
使用pipupgrade! ...最新版本 2019
pipupgrade 可帮助您从
requirements.txt
文件升级系统、本地或软件包!它还选择性地升级不会破坏更改的软件包。pipupgrade 还确保升级多个 Python 环境中存在的包。它与 Python 2.7+、Python 3.4+ 和 pip 9+、pip 10+、pip 18+、pip 19+ 兼容。
注意:我是该工具的作者。
Use pipupgrade! ... last release 2019
pipupgrade helps you upgrade your system, local or packages from a
requirements.txt
file! It also selectively upgrades packages that don't break change.pipupgrade also ensures to upgrade packages present within multiple Python environments. It is compatible with Python 2.7+, Python 3.4+ and pip 9+, pip 10+, pip 18+, pip 19+.
Note: I'm the author of the tool.
Windows 版本参考了 Rob 的优秀
FOR
文档van der Woude:您需要使用
cmd.exe
(即命令提示符);现在,Windows 默认使用 PowerShell(特别是如果您使用 Windows 终端),该命令不能直接使用该命令。或者在 PowerShell 中输入cmd
以访问命令提示符。Windows version after consulting the excellent documentation for
FOR
by Rob van der Woude:You need to use
cmd.exe
(i.e, Command Prompt); Windows nowadays defaults to PowerShell (especially if you use Windows Terminal) for which this command doesn't work directly. Or typecmd
at PowerShell to access Command Prompt.在我看来,这个选项更简单易读:
解释是
pip list --outdated
以此格式输出所有过时包的列表:在 AWK 命令,
NR>2
跳过前两条记录(行)和{print $1} 选择每行的第一个单词(根据 SergioAraujo 的建议,我删除了
tail -n +3
因为awk
确实可以处理跳过记录)。This option seems to me more straightforward and readable:
The explanation is that
pip list --outdated
outputs a list of all the outdated packages in this format:In the AWK command,
NR>2
skips the first two records (lines) and{print $1}
selects the first word of each line (as suggested by SergioAraujo, I removedtail -n +3
sinceawk
can indeed handle skipping records).以下一行可能会有所帮助:
(pip >= 22.3)
根据这个可读的答案:
或根据接受的答案:
(pip 20.0 < 22.3)
pip list --format freeze --outdated | sed 's/=.*//g' | sed 's/=.*//g' | xargs -n1 pip install -U
如果发生错误,
xargs -n1
会继续运行。如果您需要对省略的内容和引发错误的内容进行更多“细粒度”控制,则不应添加
-n1
标志并通过为每个内容“管道”以下行来显式定义要忽略的错误单独的错误:<代码> | sed 's/^<错误的第一个字符>.*//'
这是一个工作示例:
The following one-liner might prove of help:
(pip >= 22.3)
as per this readable answer:
or as per the accepted answer:
(pip 20.0 < 22.3)
pip list --format freeze --outdated | sed 's/=.*//g' | xargs -n1 pip install -U
xargs -n1
keeps going if an error occurs.If you need more "fine grained" control over what is omitted and what raises an error you should not add the
-n1
flag and explicitly define the errors to ignore, by "piping" the following line for each separate error:| sed 's/^<First characters of the error>.*//'
Here is a working example:
您可以只打印过时的包:
You can just print the packages that are outdated:
更强大的解决方案
对于pip3,请使用:
对于pip,只需删除 3,如下所示:
OS X Oddity
截至 2017 年 7 月,OS X 附带了非常旧的 sed 版本(已有十几年历史) )。要获取扩展正则表达式,请在上面的解决方案中使用
-E
而不是-r
。使用流行的解决方案解决问题
该解决方案经过精心设计和测试1,但即使是最流行的解决方案也存在问题。
上述命令使用最简单且最可移植的 pip 语法与 sed 和 sh 结合使用可以完全克服这些问题。可以使用注释版本2仔细检查sed操作的详细信息。
详细信息
[1] 在 Linux 4.8.16-200.fc24.x86_64 集群中进行测试和定期使用,并在其他五种 Linux/Unix 版本上进行测试。它还可以在 Windows 10 上安装的 Cygwin64 上运行。需要在 iOS 上进行测试。
[2] 为了更清楚地了解该命令的结构,这与上面带有注释的 pip3 命令完全相同:
[3] 升级 Python 或 PIP 组件(也用于升级 Python 或 PIP 组件)可以是死锁或包数据库损坏的潜在原因。
More Robust Solution
For pip3, use this:
For pip, just remove the 3s as such:
OS X Oddity
OS X, as of July 2017, ships with a very old version of sed (a dozen years old). To get extended regular expressions, use
-E
instead of-r
in the solution above.Solving Issues with Popular Solutions
This solution is well designed and tested1, whereas there are problems with even the most popular solutions.
The above command uses the simplest and most portable pip syntax in combination with sed and sh to overcome these issues completely. Details of the sed operation can be scrutinized with the commented version2.
Details
[1] Tested and regularly used in a Linux 4.8.16-200.fc24.x86_64 cluster and tested on five other Linux/Unix flavors. It also runs on Cygwin64 installed on Windows 10. Testing on iOS is needed.
[2] To see the anatomy of the command more clearly, this is the exact equivalent of the above pip3 command with comments:
[3] Upgrading a Python or PIP component that is also used in the upgrading of a Python or PIP component can be a potential cause of a deadlock or package database corruption.
我在升级时遇到了同样的问题。问题是,我从不升级所有软件包。我只升级我需要的,因为项目可能会中断。
因为没有简单的方法来逐个升级包并更新requirements.txt文件,所以我写了这个 pip-upgrader,它还会更新您的
requirements.txt
文件中所选软件包(或所有软件包)的版本。安装
使用
激活你的 virtualenv (很重要,因为它还会安装当前版本的升级包的新版本)虚拟环境)。
cd
进入您的项目目录,然后运行:高级用法
如果需求放置在非标准位置,请将它们作为参数发送:
如果您已经知道要升级哪个包,只需将它们发送为参数:
如果您需要升级到预发布/发布后版本,请在命令中添加
--prerelease
参数。完全披露:我写了这个包。
I had the same problem with upgrading. Thing is, I never upgrade all packages. I upgrade only what I need, because project may break.
Because there was no easy way for upgrading package by package, and updating the requirements.txt file, I wrote this pip-upgrader which also updates the versions in your
requirements.txt
file for the packages chosen (or all packages).Installation
Usage
Activate your virtualenv (important, because it will also install the new versions of upgraded packages in current virtualenv).
cd
into your project directory, then run:Advanced usage
If the requirements are placed in a non-standard location, send them as arguments:
If you already know what package you want to upgrade, simply send them as arguments:
If you need to upgrade to pre-release / post-release version, add
--prerelease
argument to your command.Full disclosure: I wrote this package.
这样看起来更简洁。
解释:
pip list --outdated
得到如下行In
cut -d ' ' -f1
,-d ' '
set "space" as分隔符,-f1
表示获取第一列。因此,上面的行变为:
然后将它们传递给 xargs 来运行命令 pip install -U,每行作为附加参数。
-n1
将传递给每个命令pip install -U
的参数数量限制为 1。This seems more concise.
Explanation:
pip list --outdated
gets lines like theseIn
cut -d ' ' -f1
,-d ' '
sets "space" as the delimiter,-f1
means to get the first column.So the above lines becomes:
Then pass them to
xargs
to run the command,pip install -U
, with each line as appending arguments.-n1
limits the number of arguments passed to each commandpip install -U
to be 1.Ramana 的回答的单行版本。
One-liner version of Ramana's answer.
来自蛋黄:
但是,您需要先获得蛋黄:
From yolk:
However, you need to get yolk first:
pip_upgrade_outdated
(基于 这个旧脚本)可以完成这项工作。根据 其文档:步骤 1:
步骤 2:
The
pip_upgrade_outdated
(based on this older script) does the job. According to its documentation:Step 1:
Step 2:
在 Windows 或 Linux 上更新 Python 包
将已安装包的列表输出到需求文件 (requirements.txt) 中:
编辑requirements.txt,并将所有“==”替换为“>=”。使用编辑器中的“全部替换”命令。
升级所有过时的软件包
来源:如何更新所有Python包
Updating Python packages on Windows or Linux
Output a list of installed packages into a requirements file (requirements.txt):
Edit requirements.txt, and replace all ‘==’ with ‘>=’. Use the ‘Replace All’ command in the editor.
Upgrade all outdated packages
Source: How to Update All Python Packages
我在 pip 问题讨论中找到的最简单、最快的解决方案是:
来源:https://github.com/pypa/pip/issues/3819
The simplest and fastest solution that I found in the pip issue discussion is:
Source: https://github.com/pypa/pip/issues/3819
当使用 virtualenv 时,如果您只想升级软件包添加对于您的 virtualenv,您可能想要执行以下操作:
When using a virtualenv and if you just want to upgrade packages added to your virtualenv, you may want to do:
Windows PowerShell 解决方案
Windows PowerShell solution
使用 AWK 更新包:
Windows PowerShell 更新
Use AWK update packages:
Windows PowerShell update
PowerShell 5.1(具有管理员权限)、Python 3.6.5 和 pip 中的一行 /em> 版本 10.0.1:
如果列表中没有损坏的软件包或特殊的轮子,则可以顺利运行...
One line in PowerShell 5.1 with administrator rights, Python 3.6.5, and pip version 10.0.1:
It works smoothly if there are no broken packages or special wheels in the list...
你可以试试这个:
You can try this:
如果您安装了
pip<22.3
,则纯 Bash/Z shell 用于实现这一目标的单行代码:或者,以一种格式良好的方式:
之后你将得到
pip>=22.3
,其中-o
和--format freeze
是互斥的,并且你不能再使用这一句。If you have
pip<22.3
installed, a pure Bash/Z shell one-liner for achieving that:Or, in a nicely-formatted way:
After this you will have
pip>=22.3
in which-o
and--format freeze
are mutually exclusive, and you can no longer use this one-liner.相当神奇的蛋黄使这一切变得容易。
有关蛋黄的更多信息:https://pypi.python.org/pypi/yolk/ 0.4.3
它可以做很多你可能会觉得有用的事情。
The rather amazing yolk makes this easy.
For more information on yolk: https://pypi.python.org/pypi/yolk/0.4.3
It can do lots of things you'll probably find useful.
使用:
Use:
Windows 上最短、最简单的。
The shortest and easiest on Windows.
没必要那么麻烦或者安装一些包。
更新 Linux shell 上的 pip 软件包:
更新 Windows powershell 上的 pip 软件包:
几点:
pip
作为您的 python 版本替换为pip3< /code> 或
pip2
。pip list --outdated
用于检查过时的 pip 包。--format
只有 3 种类型:columns(默认)、freeze 或 json。freeze
是命令管道中更好的选项。There is not necessary to be so troublesome or install some package.
Update pip packages on Linux shell:
Update pip packages on Windows powershell:
Some points:
pip
as your python version topip3
orpip2
.pip list --outdated
to check outdated pip packages.--format
on my pip version 22.0.3 only has 3 types: columns (default), freeze, or json.freeze
is better option in command pipes.Ramana 的答案对我来说效果最好,但我必须添加一些问题:
site -packages
检查排除我的开发包,因为它们不在系统 site-packages 目录中。 try- except 只是跳过已从 PyPI 中删除的包。到内膜:我希望有一个简单的< code>pip.install(dist.key,upgrade=True) 也是如此,但看起来 pip 不适合命令行以外的任何东西使用(文档没有提到内部 API ,并且 pip 开发人员没有使用文档字符串)。
Ramana's answer worked the best for me, of those here, but I had to add a few catches:
The
site-packages
check excludes my development packages, because they are not located in the system site-packages directory. The try-except simply skips packages that have been removed from PyPI.To endolith: I was hoping for an easy
pip.install(dist.key, upgrade=True)
, too, but it doesn't look like pip was meant to be used by anything but the command line (the docs don't mention the internal API, and the pip developers didn't use docstrings).这是针对 Python 3 的 PowerShell 解决方案:
对于 Python 2:
这会逐一升级软件包。因此,
事后应确保没有依赖关系被破坏。
This is a PowerShell solution for Python 3:
And for Python 2:
This upgrades the packages one by one. So a
afterwards should make sure no dependencies are broken.
这应该更有效:
pip list -o
列出过时的软件包;grep -v -i warning
对warning
进行反向匹配,以避免更新cut -f1 -d1' '
返回第一个单词 - 名称 时出现错误过时的软件包;tr "\n|\r" " "
将cut
的多行结果转换为单行、空格分隔的列表;awk '{if(NR>=3)print}'
跳过标题行cut -d' ' -f1
获取第一列xargs -n1 pip install - U
从其左侧的管道中获取 1 个参数,并将其传递给命令以升级包列表。This ought to be more effective:
pip list -o
lists outdated packages;grep -v -i warning
inverted match onwarning
to avoid errors when updatingcut -f1 -d1' '
returns the first word - the name of the outdated package;tr "\n|\r" " "
converts the multiline result fromcut
into a single-line, space-separated list;awk '{if(NR>=3)print}'
skips header linescut -d' ' -f1
fetches the first columnxargs -n1 pip install -U
takes 1 argument from the pipe left of it, and passes it to the command to upgrade the list of packages.