问题:如何使用pip更新/升级软件包?

使用pip更新软件包的方式是什么?那些不起作用:

pip update
pip upgrade

我知道这是一个简单的问题,但由于不是那么容易找到它,所以它是必需的(pip 文档不会弹出,并且堆栈溢出中的其他问题是相关的,但并不完全与此相关)

What is the way to update a package using pip? those do not work:

pip update
pip upgrade

I know this is a simple question but it is needed as it is not so easy to find (pip documentation doesn’t pop up and other questions from stack overflow are relevant but are not exactly about that)


回答 0

方式是

sudo pip install [package_name] --upgrade

或总之

sudo pip install [package_name] -U

sudo 会要求输入您的root密码以确认操作。

如果您没有root密码(如果您不是管理员),则可能应该使用virtualenv,然后删除sudo

pip install [package_name] --upgrade

The way is

pip install [package_name] --upgrade

or in short

pip install [package_name] -U

Using sudo will ask to enter your root password to confirm the action, but although common, is considered unsafe.

If you do not have a root password (if you are not the admin) you should probably work with virtualenv.

You can also use the user flag to install it on this user only.

pip install [package_name] --upgrade --user

回答 1

对于非特定软件包和更通用的解决方案,您可以签出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

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 --interactive
requests==0.14.0 is available (you have 0.13.2)
Upgrade now? [Y]es, [N]o, [A]ll, [Q]uit y

回答 2

tl; dr脚本更新所有已安装的软件包

如果只想升级一个软件包,请参考@borgr的答案。我经常觉得有必要,或者至少是令人愉快的,一次升级我的所有软件包。目前,pip本身不支持该操作,但是使用sh脚本已足够简单。您使用pip listawk(或cuttail)和命令替换。我通常的单线是:

for i in $(pip list -o | awk 'NR > 2 {print $1}'); do sudo pip install -U $i; done

这将要求输入root密码。如果您没有访问权限,则可以考虑使用pipor或virtualenv选项。

tl;dr script to update all installed packages

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:

for i in $(pip list -o | awk 'NR > 2 {print $1}'); do sudo pip install -U $i; done

This will ask for the root password. If you do not have access to that, the option of pip or virtualenv may be something to look into.


回答 3

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)

另存为xx.py
然后运行Python3 xx.py
环境:python3.5 + pip10.0 +

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+


回答 4

要为Python3.4 +升级pip,您必须使用pip3,如下所示:

sudo pip3 install pip --upgrade

这将升级位于/usr/local/lib/python3.X/dist-packages的pip

否则,要为Python2.7升级pip,请按以下方式使用pip:

sudo pip install pip --upgrade

这将升级位于以下位置的pip:/usr/local/lib/python2.7/dist-packages

To upgrade pip for Python3.4+, you must use pip3 as follows:

sudo pip3 install pip --upgrade

This will upgrade pip located at: /usr/local/lib/python3.X/dist-packages

Otherwise, to upgrade pip for Python2.7, you would use pip as follows:

sudo pip install pip --upgrade

This will upgrade pip located at: /usr/local/lib/python2.7/dist-packages


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。