是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
当前回答
下面是用Python编写脚本的另一种方法:
import pip, tempfile, contextlib
with tempfile.TemporaryFile('w+') as temp:
with contextlib.redirect_stdout(temp):
pip.main(['list', '-o'])
temp.seek(0)
for line in temp:
pk = line.split()[0]
print('--> updating', pk, '<--')
pip.main(['install', '-U', pk])
其他回答
以下一行可能会有所帮助:
(点数>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
以下是通过pip更新所有Python 3包(在激活的virtualenv中)的代码:
import pkg_resources
from subprocess import call
for dist in pkg_resources.working_set:
call("python3 -m pip install --upgrade " + dist.project_name, shell=True)
如果你在macOS上,
确保已安装Homebrew安装jq以读取您将要生成的JSON
brew install jq
更新pip3列表生成的过时包列表中的每个项目--过时
pip3 install --upgrade `pip3 list --outdated --format json | jq '.[] | .name' | awk -F'"' '{print $2}'`
Use:
pip install -r <(pip freeze) --upgrade
这似乎更简洁。
pip list --outdated | cut -d ' ' -f1 | xargs -n1 pip install -U
说明:
pip-list——过时的代码行如下
urllib3 (1.7.1) - Latest: 1.15.1 [wheel]
wheel (0.24.0) - Latest: 0.29.0 [wheel]
在cut-d“”-f1中,-d“”将“空格”设置为分隔符,-f1表示获取第一列。
因此,上述行变为:
urllib3
wheel
然后将它们传递给xargs以运行命令pipinstall-U,每一行都作为附加参数
-n1将传递给每个命令pip install-U的参数数限制为1