2

Quickly update Python packages

 2 years ago
source link: https://www.logcg.com/archives/3466.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
最近更新:26th 1月, 2021

Python 是个好东西,好多时候我们喜欢用它来写一些小东西……比如我服务器上的很多服务都是用 Python 写的( 使用 Supervisor 将你的 Python 程序变成服务 ),日积月累之下,竟然用到了很多依赖包。

可是,这些 Python 的依赖包并不会随着你的 apt upgrade  进行更新,许久之后的今天,我发现某个我正在用的包,已经更新到了 1.10.x,而我,还在用着0.4……

总之,Python 自带的包管理器并没有提供一个方便的命令类似 apt upgrade 来一键更新所有包,我们得想一个其他的办法。

一行命令更新法

网上有的文章使用了一个复杂的命令进行更新,原理是读取 pip3 list --outdated (讽刺的是 pip 给出了过期包列表,却不允许你一键升级它们)结果再依次输入给 pip3 install -U ,这个方案并不那么稳定,也许精通shell的高手能将它运用自如吧,我就不提供具体代码了,因为每个人的实现都有那么一点不同。

使用 pip-review 更新

不要使用下文中的 requirements 了,并不好用,使用 pip-review 可挨个设置和选择要更新的依赖包,然后批量更新,方便快捷。

$ pip3 install pip-review

安装 pip-review 后,使用如下命令使用:

python3 -m pip_review --local --interactive

使用 requirements

有没有更简单易懂的更新方案呢?答案是有的。

requirement 文件是用来解决 Python 包分发一键配置环境依赖用的,我们也可以将全局环境打印出来,然后再以更新的形式导入,这样,就搞定了全局更新!

<del>pip3 freeze > requirements.txt</del>

首先导出全局依赖——也就是全部已经安装的包。

<del>pip3 install -r requirements.txt --upgrade</del>

现在我们把这些包重新“安装”回去,但要 upgrade ……完成!

本文由 落格博客 原创撰写:落格博客 » 快速更新 Python 包

转载请保留出处和原文链接:https://www.logcg.com/archives/3466.html


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK