是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
当前回答
拉玛纳的回答对我来说是最好的,但我不得不补充几点:
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)。
其他回答
这里发布的所有解决方案都打破了依赖关系。
在此对话中,将功能直接包含到pip中,包括正确管理依赖关系:
的作者https://github.com/kdeldycke/meta-package-manager写入MPM可以模拟缺少的upgrade all命令:
mpm --pip upgrade --all
以下命令在Windows上运行,对其他人也很有用($是命令提示符下的任何目录。例如,C:\Users/Username)。
Do
$ pip freeze > requirements.txt
打开文本文件,将==替换为>=,或让sed为您执行以下操作:
$ sed -i 's/==/>=/g' requirements.txt
并执行:
$ pip install -r requirements.txt --upgrade
如果您对某个软件包延迟升级有问题(有时是NumPy),只需转到目录($),注释掉名称(在其前面添加#),然后再次运行升级。稍后可以取消注释该部分。这对于复制Python全局环境也非常有用。
另一种方式:
我也喜欢pip审查方法:
py2
$ pip install pip-review
$ pip-review --local --interactive
py3
$ pip3 install pip-review
$ py -3 -m pip-review --local --interactive
您可以选择“a”来升级所有软件包;如果一次升级失败,请再次运行,然后在下一次升级中继续。
通过拉取请求发送给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 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
拉玛纳的回答对我来说是最好的,但我不得不补充几点:
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)。