是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
当前回答
具有管理员权限的PowerShell 5.1、Python 3.6.5和pip版本10.0.1中的一行:
pip list -o --format json | ConvertFrom-Json | foreach {pip install $_.name -U --no-warn-script-location}
如果列表中没有破损的包裹或特殊的轮子,它会顺利工作。。。
其他回答
我喜欢使用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包维护之外,非常简单地用于升级。
拉玛纳的回答对我来说是最好的,但我不得不补充几点:
import pip
for dist in pip.get_installed_distributions():
if 'site-packages' in dist.location:
try:
pip.call_subprocess(['pip', 'install', '-U', dist.key])
except Exception, exc:
print exc
站点包检查排除了我的开发包,因为它们不在系统站点包目录中。try-except只是跳过已从PyPI中删除的包。
对于endolith:我也希望有一个简单的pip.install(dist.key,upgrade=True),但它看起来不像是要让命令行以外的任何东西使用pip(文档没有提到内部API,pip开发人员也没有使用docstring)。
使用virtualenv时,如果您只想升级添加到virtualenv的软件包,您可能需要执行以下操作:
pip install `pip freeze -l | cut --fields=1 -d = -` --upgrade
以下是通过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)
这似乎对我有用。。。
pip install -U $(pip list --outdated | awk '{printf $1" "}')
之后,我使用带有空格的printf来正确分隔包名。