编程语言
首页 > 编程语言> > Python 如何更新所有包?

Python 如何更新所有包?

作者:互联网

总结自Stackoverflow:How to upgrade all Python packages with pip

@

目录

方法一:pip命令

温馨提示:此命令仅适于Linux用户

pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip install -U

命令很长,我们来分析一下其中的各个参数

pip list --outdated 可以列出过时的python包

pip list --format 用来指定输出格式,其中freeze格式,输出是类似这样的

attrs==21.2.0
Automat==20.2.0
blinker==1.4
certifi==2021.5.30
cffi==1.14.6
chardet==4.0.0
charset-normalizer==2.0.4
click==8.0.1
cloud-init==21.2
colorama==0.4.4
command-not-found==0.3

是不是有点像requirements.txt文件的内容?

后面就是Linux命令了,用grep 过滤掉-e开头的包,接着传给cut命令,切掉每行==后面的所有内容,输出是类似这样的

attrs
Automat
blinker
certifi
cffi
chardet
charset-normalizer
click
cloud-init
colorama
command-not-found

最后的xargs接收前面的输出,传给pip install -U执行,通过指定-n1参数,使得输出的每一行都作为参数执行一次pip install -U

方法二:pip-review

pip-review相当于一个简单包装过的pip。它可以像 pip list --outdated 一样列出可更新的包。还能像使用 pip install 命令一样自动或者交互式的安装可更新的包。

比如,如果只是想查看一下:

$ pip-review
requests==0.13.4 is available (you have 0.13.2)
redis==2.4.13 is available (you have 2.4.9)
rq==0.3.2 is available (you have 0.3.0)

或者,更新所有包:

$ pip-review --auto
... <pip install output>

再者,交互式的运行,在每个包更新之前,询问一下:

$ pip-review --interactive
requests==0.14.0 is available (you have 0.13.2)
Upgrade now? [Y]es, [N]o, [A]ll, [Q]uit y
...
redis==2.6.2 is available (you have 2.4.9)
Upgrade now? [Y]es, [N]o, [A]ll, [Q]uit n
rq==0.3.2 is available (you have 0.3.0)
Upgrade now? [Y]es, [N]o, [A]ll, [Q]uit y
...

运行 pip-review -h 可以查看完整的选项,或者去GitHUb:https://github.com/jgonggrijp/pip-review

注意:如果想固定特定的包以防止它们自动升级,可以搞一个约束文件(比如:requirements.txt

$ export PIP_CONSTRAINT="${HOME}/constraints.txt
$ cat $PIP_CONSTRAINT
pyarrow==0.14.1
pandas<0.24.0

$ pip-review --auto
...

方法三:pipupgrade

pipupgrade的功能和pip-review类似,不过它的命令行界面倒是挺炫酷的,比较符合我的审美哈。

查看所有可更新的包:

$ pipupgrade --check
Checking...
Source: Installed Distributions (/home/pineapple/.local/bin/pip)

Name            Current Version Latest Version Home Page                                
--------------- --------------- -------------- -----------------------------------------
decorator       4.4.2           5.0.9          https://github.com/micheles/decorator    
humanize        0.5.1           3.11.0         https://github.com/jmoiron/humanize      
pid             2.2.3           3.0.4          https://github.com/trbs/pid/             
pycairo         1.20.0          1.20.1         https://pycairo.readthedocs.io           
pyenchant       3.2.0           3.2.1          https://pyenchant.github.io/pyenchant/   
pykickstart     3.32            3.34           http://fedoraproject.org/wiki/pykickstart
python-augeas   0.5.0           1.1.0          http://augeas.net/                       
python-dateutil 2.8.1           2.8.2          https://github.com/dateutil/dateutil     
requests        2.25.1          2.26.0         https://requests.readthedocs.io          
setuptools      53.0.0          57.4.0         https://github.com/pypa/setuptools       
six             1.15.0          1.16.0         https://github.com/benjaminp/six         
slip            0.6.4           20191113       https://github.com/bicv/SLIP             
soupsieve       2.2             2.2.1          https://github.com/facelessuser/soupsieve
Tempita         0.5.1           0.5.2          http://pythonpaste.org/tempita/          
urllib3         1.25.10         1.26.6         https://urllib3.readthedocs.io/
......

很炫酷啊!

更新所有的包:

$ pipupgrade

交互式的更新:

$ pipupgrade --interactive

更多pipupgrade 选项可以用 pipupgrade -h 查看,或者去GitHub:https://github.com/achillesrasquinha/pipupgrade

标签:github,Python,review,所有,更新,--,https,pip,com
来源: https://www.cnblogs.com/james-wangx/p/16111449.html