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

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


当前回答

如果您希望升级仅由pip安装,并且避免升级由其他工具(如apt、yum等)安装的软件包,那么您可以使用我在Ubuntu上使用的脚本(可能也适用于其他发行版)-基于以下帖子:

printf "To update with pip: pip install -U"
pip list --outdated 2>/dev/null | gawk '{print $1;}' | while read; do pip show "${REPLY}" 2>/dev/null | grep 'Location: /usr/local/lib/python2.7/dist-packages' >/dev/null; if (( $? == 0 )); then printf " ${REPLY}"; fi; done; echo

其他回答

这是我对rbp答案的变体,它绕过了“可编辑”和开发发行版。它有两个缺点:不必要地重新下载和重新安装;并且一个包上的错误将阻止之后每个包的升级。

pip freeze |sed -ne 's/==.*//p' |xargs pip install -U --

相关错误报告,从Bitbucket迁移后有点脱节:

https://github.com/pypa/pip/issues/49https://github.com/pypa/pip/issues/59

这似乎更简洁。

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

通过拉取请求发送给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

Windows上最短、最简单的。

pip freeze > requirements.txt && pip install --upgrade -r requirements.txt && rm requirements.txt

您可以只打印过期的软件包:

pip freeze | cut -d = -f 1 | xargs -n 1 pip search | grep -B2 'LATEST:'