是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
当前回答
python -c 'import pip; [pip.main(["install", "--upgrade", d.project_name]) for d in pip.get_installed_distributions()]'
一行!
其他回答
使用pipupgrade。。。最新版本2019
$ pip install pipupgrade
$ pipupgrade --verbose --latest --yes
pipupgrade帮助您从requirements.txt文件升级系统、本地或软件包!它还选择性地升级不破坏更改的包。
pipupgrade还确保升级多个Python环境中的包。它与Python2.7+、Python23.4+和pip9+、pip10+、pip18+、pip19+兼容。
注:我是该工具的作者。
通过拉取请求发送给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
以下是通过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)
下面是用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])
pip_upgrade_outdated(基于此旧脚本)完成此任务。根据其文件:
usage: pip_upgrade_outdated [-h] [-3 | -2 | --pip_cmd PIP_CMD]
[--serial | --parallel] [--dry_run] [--verbose]
[--version]
Upgrade outdated python packages with pip.
optional arguments:
-h, --help show this help message and exit
-3 use pip3
-2 use pip2
--pip_cmd PIP_CMD use PIP_CMD (default pip)
--serial, -s upgrade in serial (default)
--parallel, -p upgrade in parallel
--dry_run, -n get list, but don't upgrade
--verbose, -v may be specified multiple times
--version show program's version number and exit
步骤1:
pip install pip-upgrade-outdated
第2步:
pip_upgrade_outdated