是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
当前回答
以下一行可能会有所帮助:
(点数>20.00)
pip list--格式冻结--过时|sed的/=.*//g'| xargs-n1 pip install-U
旧版本:pip list--格式冻结--过时|sed的/(.*//g'| xargs-n1 pip install-U
如果发生错误,xargs-n1将继续运行。
如果您需要对省略的内容和引发错误的内容进行更多的“细粒度”控制,则不应添加-n1标志并明确定义要忽略的错误,方法是为每个单独的错误“管道化”以下行:
|sed的/^<错误的第一个字符>.*//'
下面是一个工作示例:
pip list --format freeze --outdated | sed 's/=.*//g' | sed 's/^<First characters of the first error>.*//' | sed 's/^<First characters of the second error>.*//' | xargs pip install -U
其他回答
这里有一个脚本,它只更新过时的包。
import os, sys
from subprocess import check_output, call
file = check_output(["pip.exe", "list", "--outdated", "--format=legacy"])
line = str(file).split()
for distro in line[::6]:
call("pip install --upgrade " + distro, shell=True)
对于不输出为传统格式的新版本pip(版本18+):
import os, sys
from subprocess import check_output, call
file = check_output(["pip.exe", "list", "-o", "--format=json"])
line = str(file).split()
for distro in line[1::8]:
distro = str(distro).strip('"\",')
call("pip install --upgrade " + distro, shell=True)
pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U
我喜欢使用pip工具来处理这个过程。
打包pip工具提供两个脚本:
pipcompile:用于从requirement.in文件创建可行的requirements.txt。pip-sync:用于同步本地环境pip存储库以匹配requirements.txt文件。
如果我想升级特定软件包,请说:
django==3.2.12
to
django==3.2.16
然后,我可以在requirements.in中更改django的基本版本,运行pipcompile,然后运行pipsync。这将通过删除旧版本,然后安装新版本,有效地升级django(以及所有依赖的软件包)。
除了pip包维护之外,非常简单地用于升级。
从…起https://github.com/cakebread/yolk:
$ pip install -U `yolk -U | awk '{print $1}' | uniq`
然而,你需要先得到蛋黄:
$ sudo pip install -U yolk
更稳健的解决方案
对于pip3,请使用以下命令:
pip3 freeze --local |sed -rn 's/^([^=# \t\\][^ \t=]*)=.*/echo; echo Processing \1 ...; pip3 install -U \1/p' |sh
对于pip,只需删除3s即可:
pip freeze --local |sed -rn 's/^([^=# \t\\][^ \t=]*)=.*/echo; echo Processing \1 ...; pip install -U \1/p' |sh
OS X奇数
截至2017年7月,OS X附带了一个非常旧的sed版本(已有十几年历史)。要获得扩展正则表达式,请在上面的解决方案中使用-E而不是-r。
使用流行解决方案解决问题
这个解决方案经过了精心设计和测试1,而即使是最流行的解决方案也存在问题。
由于pip命令行特性的变化而导致的可移植性问题由于常见的pip或pip3子进程故障导致xargs崩溃来自原始xargs输出的拥挤日志记录依赖Python到OS桥,同时可能升级它3
上面的命令使用最简单和最可移植的pip语法,并结合sed和sh来完全解决这些问题。sed操作的详细信息可以使用注释的版本2进行详细检查。
细节
[1] 在Linux 4.8.16-200.fc24.x86_64集群中测试并定期使用,并在其他五种Linux/Unix版本上测试。它还可以在Windows 10上安装的Cygwin64上运行。需要在iOS上进行测试。
[2] 为了更清楚地看到命令的解剖结构,这与上面的pip3命令完全等价,并带有注释:
# Match lines from pip's local package list output
# that meet the following three criteria and pass the
# package name to the replacement string in group 1.
# (a) Do not start with invalid characters
# (b) Follow the rule of no white space in the package names
# (c) Immediately follow the package name with an equal sign
sed="s/^([^=# \t\\][^ \t=]*)=.*"
# Separate the output of package upgrades with a blank line
sed="$sed/echo"
# Indicate what package is being processed
sed="$sed; echo Processing \1 ..."
# Perform the upgrade using just the valid package name
sed="$sed; pip3 install -U \1"
# Output the commands
sed="$sed/p"
# Stream edit the list as above
# and pass the commands to a shell
pip3 freeze --local | sed -rn "$sed" | sh
[3] 升级也用于升级Python或PIP组件的Python或PIP组件可能会导致死锁或包数据库损坏。