Python 如何更新所有包?
<blockquote><p>总结自Stackoverflow:How to upgrade all Python packages with pip</p>
</blockquote>
<p>@</p><div class="toc"><div class="toc-container-header">目录</div><ul><li>方法一:pip命令</li><li>方法二:pip-review</li><li>方法三:pipupgrade</li></ul></div><p></p>
<h2 id="方法一pip命令">方法一:pip命令</h2>
<p>温馨提示:此命令仅适于Linux用户</p>
<pre><code class="language-bash">pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1| xargs -n1 pip install -U
</code></pre>
<p>命令很长,我们来分析一下其中的各个参数</p>
<p><code>pip list --outdated</code> 可以列出过时的python包</p>
<p><code>pip list --format</code> 用来指定输出格式,其中<code>freeze</code>格式,输出是类似这样的</p>
<pre><code class="language-bash">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
</code></pre>
<p>是不是有点像requirements.txt文件的内容?</p>
<p>后面就是Linux命令了,用<code>grep</code> 过滤掉-e开头的包,接着传给<code>cut</code>命令,切掉每行==后面的所有内容,输出是类似这样的</p>
<pre><code class="language-bash">attrs
Automat
blinker
certifi
cffi
chardet
charset-normalizer
click
cloud-init
colorama
command-not-found
</code></pre>
<p>最后的<code>xargs</code>接收前面的输出,传给<code>pip install -U</code>执行,通过指定<code>-n1</code>参数,使得输出的每一行都作为参数执行一次<code>pip install -U</code></p>
<h2 id="方法二pip-review">方法二:pip-review</h2>
<p>pip-review相当于一个简单包装过的pip。它可以像 <code>pip list --outdated</code> 一样列出可更新的包。还能像使用 <code>pip install</code> 命令一样自动或者交互式的安装可更新的包。</p>
<p>比如,如果只是想查看一下:</p>
<pre><code class="language-bash">$ 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)
</code></pre>
<p>或者,更新所有包:</p>
<pre><code class="language-bash">$ pip-review --auto
... <pip install output>
</code></pre>
<p>再者,交互式的运行,在每个包更新之前,询问一下:</p>
<pre><code class="language-bash">$ pip-review --interactive
requests==0.14.0 is available (you have 0.13.2)
Upgrade now? es, o, ll, uit y
...
redis==2.6.2 is available (you have 2.4.9)
Upgrade now? es, o, ll, uit n
rq==0.3.2 is available (you have 0.3.0)
Upgrade now? es, o, ll, uit y
...
</code></pre>
<p>运行 <code>pip-review -h</code> 可以查看完整的选项,或者去GitHUb:https://github.com/jgonggrijp/pip-review</p>
<p>注意:如果想固定特定的包以防止它们自动升级,可以搞一个约束文件(比如:<code>requirements.txt</code>)</p>
<pre><code class="language-bash">$ export PIP_CONSTRAINT="${HOME}/constraints.txt
$ cat $PIP_CONSTRAINT
pyarrow==0.14.1
pandas<0.24.0
$ pip-review --auto
...
</code></pre>
<h2 id="方法三pipupgrade">方法三:pipupgrade</h2>
<p>pipupgrade的功能和pip-review类似,不过它的命令行界面倒是挺炫酷的,比较符合我的审美哈。</p>
<p>查看所有可更新的包:</p>
<pre><code class="language-bash">$ 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/
......
</code></pre>
<p>很炫酷啊!</p>
<p>更新所有的包:</p>
<pre><code class="language-bash">$ pipupgrade
</code></pre>
<p>交互式的更新:</p>
<pre><code class="language-bash">$ pipupgrade --interactive
</code></pre>
<p>更多pipupgrade 选项可以用 <code>pipupgrade -h</code> 查看,或者去GitHub:https://github.com/achillesrasquinha/pipupgrade</p><br><br>
来源:https://www.cnblogs.com/james-wangx/p/16111449.html
頁:
[1]