Python 如何使用 pip 更新/升级包?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47071256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to update/upgrade a package using pip?
提问by borgr
What is the way to update a package using pip? those do not work:
使用 pip 更新包的方法是什么?那些不起作用:
pip update
pip upgrade
I know this is a simple question but it is needed as it is not so easy to find (pip documentationdoesn't pop up and other questions from stack overflow are relevant but are not exactly about that)
我知道这是一个简单的问题,但它是必需的,因为它不是那么容易找到(pip文档不会弹出,堆栈溢出中的其他问题是相关的,但不完全是这样)
回答by borgr
The way is
办法是
sudo pip install [package_name] --upgrade
or in short
或者简而言之
sudo pip install [package_name] -U
sudo
will ask to enter your root password to confirm the action.
sudo
将要求输入您的 root 密码以确认操作。
If you do not have a root password (if you are not the admin) you should probably work with virtualenvand then you should drop the sudo
:
如果您没有 root 密码(如果您不是管理员),您可能应该使用virtualenv,然后您应该删除sudo
:
pip install [package_name] --upgrade
回答by Aiden Woodruff
tl;dr script to update all installed packages
tl;dr 脚本来更新所有已安装的软件包
If you only want to upgrade one package, refer to @borgr's answer. I often find it necessary, or at least pleasing, to upgrade all my packages at once. Currently, pip doesn't natively support that action, but with sh scripting it is simple enough. You use pip list
, awk
(or cut
and tail
), and command substitution. My normal one-liner is:
如果您只想升级一个软件包,请参阅@borgr 的回答。我经常发现一次升级我所有的软件包是必要的,或者至少是令人愉快的。目前,pip 本身并不支持该操作,但使用 sh 脚本就足够简单了。您使用pip list
, awk
(或cut
和tail
)和命令替换。我的正常单线是:
for i in $(pip list -o | awk 'NR > 2 {print }'); do sudo pip install -U $i; done
This will ask for the root password. If you do not have access to that, the --user
option of pip
or virtualenvmay be something to look into.
这将要求输入 root 密码。如果您无权访问它,则可能需要--user
研究pip
或virtualenv的选项。
回答by as - if
For a non-specific package and a more general solution you can check out pip-review, a tool that checks what packages could/should be updated.
对于非特定包和更通用的解决方案,您可以查看pip-review,这是一种检查可以/应该更新哪些包的工具。
$ 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
回答by Nick D
To upgrade pip for Python3.4+, you must use pip3 as follows:
为 Python3.4+ 升级 pip,你必须使用 pip3 如下:
sudo pip3 install pip --upgrade
This will upgrade pip located at: /usr/local/lib/python3.X/dist-packages
这将升级位于:/usr/local/lib/python3.X/dist-packages 的 pip
Otherwise, to upgrade pip for Python2.7, you would use pip as follows:
否则,要升级 Python2.7 的 pip,您将按如下方式使用 pip:
sudo pip install pip --upgrade
This will upgrade pip located at: /usr/local/lib/python2.7/dist-packages
这将升级位于:/usr/local/lib/python2.7/dist-packages 的 pip
回答by Fofdsf
import subprocess as sbp
import pip
pkgs = eval(str(sbp.run("pip3 list -o --format=json", shell=True,
stdout=sbp.PIPE).stdout, encoding='utf-8'))
for pkg in pkgs:
sbp.run("pip3 install --upgrade " + pkg['name'], shell=True)
Save as xx.py
Then run Python3 xx.py
Environment: python3.5+ pip10.0+
另存为 xx.py
然后运行 Python3 xx.py
环境:python3.5+ pip10.0+