问题:如何使用pip升级所有Python软件包?

是否可以一次升级所有Python软件包pip

注意:官方问题追踪器上对此功能有要求

Is it possible to upgrade all Python packages at one time with pip?

Note: that there is a feature request for this on the official issue tracker.


回答 0

还没有内置标志,但是您可以使用

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

注意:为此存在无限的潜在变化。我试图使这个答案简短而简单,但是请在评论中提出一些建议!

在的旧版本中pip,您可以改用以下代码:

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

grep@jawache所建议的,该命令将跳过可编辑的(“ -e”)程序包定义。(是的,您可以将grep+ 替换cutsedor awkperlor or …)。

-n1标志用于xargs防止在更新一个软件包失败时停止所有操作(感谢@andsens)。

There isn’t a built-in flag yet, but you can use

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

Note: there are infinite potential variations for this. I’m trying to keep this answer short and simple, but please do suggest variations in the comments!

In older version of pip, you can use this instead:

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

The grep is to skip editable (“-e”) package definitions, as suggested by @jawache. (Yes, you could replace grep+cut with sed or awk or perl or…).

The -n1 flag for xargs prevents stopping everything if updating one package fails (thanks @andsens).


回答 1

您可以使用以下Python代码。与不同pip freeze,这不会打印警告和FIXME错误。 对于点<10.0.1

import pip
from subprocess import call

packages = [dist.project_name for dist in pip.get_installed_distributions()]
call("pip install --upgrade " + ' '.join(packages), shell=True)

对于点> = 10.0.1

import pkg_resources
from subprocess import call

packages = [dist.project_name for dist in pkg_resources.working_set]
call("pip install --upgrade " + ' '.join(packages), shell=True)

You can use the following Python code. Unlike pip freeze, this will not print warnings and FIXME errors. For pip < 10.0.1

import pip
from subprocess import call

packages = [dist.project_name for dist in pip.get_installed_distributions()]
call("pip install --upgrade " + ' '.join(packages), shell=True)

For pip >= 10.0.1

import pkg_resources
from subprocess import call

packages = [dist.project_name for dist in pkg_resources.working_set]
call("pip install --upgrade " + ' '.join(packages), shell=True)

回答 2

升级所有本地软件包;您可以使用pip-review

$ pip install pip-review
$ pip-review --local --interactive

pip-review是的叉子pip-tools。见pip-tools问题被提到@knedlsepppip-review包有效,但pip-tools包不再有效。

pip-review从0.5版开始在Windows上运行。

To upgrade all local packages; you could use pip-review:

$ pip install pip-review
$ pip-review --local --interactive

pip-review is a fork of pip-tools. See pip-tools issue mentioned by @knedlsepp. pip-review package works but pip-tools package no longer works.

pip-review works on Windows since version 0.5.


回答 3

适用于Windows。也应该对别人有好处。($是您在命令提示符下所在的目录,例如C:/ Users / Username>)

$ pip freeze > requirements.txt

打开文本文件,替换==>=,并执行

$ pip install -r requirements.txt --upgrade

如果您对某个软件包停止升级有问题(有时为numpy),则只需转到目录($),注释掉名称(在其前面添加#),然后再次运行升级。您稍后可以取消对该部分的注释。这对于复制python全局环境也非常有用。


另一种方式:

我也喜欢pip-review方法:

py2
$ pip install pip-review

$ pip-review --local --interactive

py3
$ pip3 install pip-review

$ py -3 -m pip_review --local --interactive

您可以选择“ a”来升级所有软件包。如果一次升级失败,请再次运行它,然后继续进行下一次升级。

Works on Windows. Should be good for others too. ($ is whatever directory you’re in, in command prompt. eg. C:/Users/Username>)

do

$ pip freeze > requirements.txt

open the text file, replace the == with >= , and execute

$ pip install -r requirements.txt --upgrade

If you have a problem with a certain package stalling the upgrade (numpy sometimes), just go to the directory ($), comment out the name (add a # before it) and run the upgrade again. You can later uncomment that section back. This is also great for copying python global environments.


Another way:

I also like the pip-review method:

py2
$ pip install pip-review

$ pip-review --local --interactive

py3
$ pip3 install pip-review

$ py -3 -m pip_review --local --interactive

You can select ‘a’ to upgrade all packages; if one upgrade fails, run it again and it continues at the next one.


回答 4

咨询优良的售后服务Windows版本文档FOR罗布范德Woude

for /F "delims===" %i in ('pip freeze -l') do pip install -U %i

Windows version after consulting excellent documentation for FOR by Rob van der Woude

for /F "delims===" %i in ('pip freeze -l') do pip install -U %i

回答 5

使用pipupgrade

$ pip install pipupgrade
$ pipupgrade --verbose --latest --yes

pipupgrade可帮助您从requirements.txt文件升级系统,本地或软件包!它还有选择地升级不会破坏更改的软件包。pipupgrade还确保升级存在于多个Python环境中的软件包。与Python2.7 +,Python3.4 +和pip9 +,pip10 +,pip18 +,pip19 +兼容。

在此处输入图片说明

注意:我是该工具的作者。

Use pipupgrade!

$ pip install pipupgrade
$ pipupgrade --verbose --latest --yes

pipupgrade helps you upgrade your system, local or packages from a requirements.txt file! It also selectively upgrades packages that don’t break change. pipupgrade also ensures to upgrade packages present within multiple Python environments. Compatible with Python2.7+, Python3.4+ and pip9+, pip10+, pip18+, pip19+.

enter image description here

NOTE: I’m the author of the tool.


回答 6

您可以只打印过时的软件包

pip freeze | cut -d = -f 1 | xargs -n 1 pip search | grep -B2 'LATEST:'

You can just print the packages that are outdated

pip freeze | cut -d = -f 1 | xargs -n 1 pip search | grep -B2 'LATEST:'

回答 7

在我看来,此选项更直接易读:

pip install -U `pip list --outdated | awk 'NR>2 {print $1}'`

解释是以pip list --outdated这种格式输出所有过时软件包的列表:

Package   Version Latest Type 
--------- ------- ------ -----
fonttools 3.31.0  3.32.0 wheel
urllib3   1.24    1.24.1 wheel
requests  2.20.0  2.20.1 wheel

在awk命令中,NR>2跳过前两个记录(行)并{print $1}选择每行的第一个单词(如SergioAraujo所建议,我删除了它,tail -n +3因为awk它确实可以处理跳过的记录)。

This option seems to me more straightforward and readable:

pip install -U `pip list --outdated | awk 'NR>2 {print $1}'`

The explanation is that pip list --outdated outputs a list of all the outdated packages in this format:

Package   Version Latest Type 
--------- ------- ------ -----
fonttools 3.31.0  3.32.0 wheel
urllib3   1.24    1.24.1 wheel
requests  2.20.0  2.20.1 wheel

In the awk command, NR>2 skips the first two records (lines) and {print $1} selects the first word of each line (as suggested by SergioAraujo, I removed tail -n +3 since awk can indeed handle skipping records).


回答 8

以下一线可能会有所帮助:

(点> 20.0)

pip list --format freeze --outdated | sed 's/=.*//g' | xargs -n1 pip install -U

旧版本:

pip list --format freeze --outdated | sed 's/(.*//g' | xargs -n1 pip install -U

xargs -n1 继续发生错误。

如果您需要对遗漏的内容和引起错误的内容进行更多的“细粒度”控制,则不应添加-n1标记并显式定义要忽略的错误,方法是为每个单独的错误“插入”以下行:

| sed 's/^<First characters of the error>.*//'

这是一个工作示例:

pip list --format freeze --outdated | sed 's/=.*//g' | sed 's/^<First characters of the first error>.*//' | sed 's/^<First characters of the second error>.*//' | xargs pip install -U

The following one-liner might prove of help:

(pip > 20.0)

pip list --format freeze --outdated | sed 's/=.*//g' | xargs -n1 pip install -U

Older Versions:

pip list --format freeze --outdated | sed 's/(.*//g' | xargs -n1 pip install -U

xargs -n1 keeps going if an error occurs.

If you need more “fine grained” control over what is omitted and what raises an error you should not add the -n1 flag and explicitly define the errors to ignore, by “piping” the following line for each separate error:

| sed 's/^<First characters of the error>.*//'

Here is a working example:

pip list --format freeze --outdated | sed 's/=.*//g' | sed 's/^<First characters of the first error>.*//' | sed 's/^<First characters of the second error>.*//' | xargs pip install -U

回答 9

更强大的解决方案

对于pip3,请使用以下命令:

pip3 freeze --local |sed -rn 's/^([^=# \t\\][^ \t=]*)=.*/echo; echo Processing \1 ...; pip3 install -U \1/p' |sh

对于点子,只需将3删除即可:

pip freeze --local |sed -rn 's/^([^=# \t\\][^ \t=]*)=.*/echo; echo Processing \1 ...; pip install -U \1/p' |sh

OSX奇数

截至2017年7月,OSX随附了非常老版本的sed(已有十二年历史)。要获取扩展的正则表达式,请在上述解决方案中使用-E而不是-r。

用流行的解决方案解决问题

该解决方案经过精心设计和测试1,而即使是最流行的解决方案也存在问题。

  • 由于更改pip命令行功能而导致的可移植性问题
  • 由于常见的pip或pip3子进程失败而导致xargs崩溃
  • 来自原始xargs输出的拥挤日志
  • 依靠Python到OS的网桥,同时可能对其进行升级3

上面的命令结合使用sed和sh来使用最简单,最可移植的pip语法来完全解决这些问题。sed操作的详细信息可以通过注释的版本2进行审查。


细节

[1]经过测试并在Linux 4.8.16-200.fc24.x86_64群集中正常使用,并在其他五种Linux / Unix版本上进行了测试。它还可以在Windows 10上安装的Cygwin64上运行。需要在iOS上进行测试。

[2]为了更清楚地了解命令的结构,这与上面带有注释的pip3命令完全等效:

# match lines from pip's local package list output
# that meet the following three criteria and pass the
# package name to the replacement string in group 1.
# (a) Do not start with invalid characters
# (b) Follow the rule of no white space in the package names
# (c) Immediately follow the package name with an equal sign
sed="s/^([^=# \t\\][^ \t=]*)=.*"

# separate the output of package upgrades with a blank line
sed="$sed/echo"

# indicate what package is being processed
sed="$sed; echo Processing \1 ..."

# perform the upgrade using just the valid package name
sed="$sed; pip3 install -U \1"

# output the commands
sed="$sed/p"

# stream edit the list as above
# and pass the commands to a shell
pip3 freeze --local |sed -rn "$sed" |sh

[3]升级还用于升级Python或PIP组件的Python或PIP组件可能是导致死锁或软件包数据库损坏的潜在原因。

More Robust Solution

For pip3 use this:

pip3 freeze --local |sed -rn 's/^([^=# \t\\][^ \t=]*)=.*/echo; echo Processing \1 ...; pip3 install -U \1/p' |sh

For pip, just remove the 3s as such:

pip freeze --local |sed -rn 's/^([^=# \t\\][^ \t=]*)=.*/echo; echo Processing \1 ...; pip install -U \1/p' |sh

OSX Oddity

OSX, as of July 2017, ships with a very old version of sed (a dozen years old). To get extended regular expressions, use -E instead of -r in the solution above.

Solving Issues with Popular Solutions

This solution is well designed and tested1, whereas there are problems with even the most popular solutions.

  • Portability issues due to changing pip command line features
  • Crashing of xargs because common pip or pip3 child process failures
  • Crowded logging from the raw xargs output
  • Relying on a Python-to-OS bridge while potentially upgrading it3

The above command uses the simplest and most portable pip syntax in combination with sed and sh to overcome these issues completely. Details of sed operation can be scrutinized with the commented version2.


Details

[1] Tested and regularly used in a Linux 4.8.16-200.fc24.x86_64 cluster and tested on five other Linux/Unix flavors. It also runs on Cygwin64 installed on Windows 10. Testing on iOS is needed.

[2] To see the anatomy of the command more clearly, this is the exact equivalent of the above pip3 command with comments:

# match lines from pip's local package list output
# that meet the following three criteria and pass the
# package name to the replacement string in group 1.
# (a) Do not start with invalid characters
# (b) Follow the rule of no white space in the package names
# (c) Immediately follow the package name with an equal sign
sed="s/^([^=# \t\\][^ \t=]*)=.*"

# separate the output of package upgrades with a blank line
sed="$sed/echo"

# indicate what package is being processed
sed="$sed; echo Processing \1 ..."

# perform the upgrade using just the valid package name
sed="$sed; pip3 install -U \1"

# output the commands
sed="$sed/p"

# stream edit the list as above
# and pass the commands to a shell
pip3 freeze --local |sed -rn "$sed" |sh

[3] Upgrading a Python or PIP component that is also used in the upgrading of a Python or PIP component can be a potential cause of a deadlock or package database corruption.


回答 10

这似乎更简洁。

pip list --outdated | cut -d ' ' -f1 | xargs -n1 pip install -U

说明:

pip list --outdated 得到这样的线

urllib3 (1.7.1) - Latest: 1.15.1 [wheel]
wheel (0.24.0) - Latest: 0.29.0 [wheel]

在中cut -d ' ' -f1-d ' '将“空格”设置为定界符,-f1表示获取第一列。

因此,以上几行变为:

urllib3
wheel

然后将它们传递xargs给运行命令pip install -U,并将每行作为附加参数

-n1将传递给每个命令的参数数量限制pip install -U为1

This seems more concise.

pip list --outdated | cut -d ' ' -f1 | xargs -n1 pip install -U

Explanation:

pip list --outdated gets lines like these

urllib3 (1.7.1) - Latest: 1.15.1 [wheel]
wheel (0.24.0) - Latest: 0.29.0 [wheel]

In cut -d ' ' -f1, -d ' ' sets “space” as the delimiter, -f1 means to get the first column.

So the above lines becomes:

urllib3
wheel

then pass them to xargs to run the command, pip install -U, with each line as appending arguments

-n1 limits the number of arguments passed to each command pip install -U to be 1


回答 11

我在升级时遇到了同样的问题。问题是,我从不升级所有软件包。我只升级我需要的东西,因为项目可能会中断。

因为没有简便的方法来逐个软件包升级软件包和更新requirements.txt文件,所以我写了这个pip-upgrader,它requirements.txt为所选软件包(或所有软件包)更新了文件中的版本

安装

pip install pip-upgrader

用法

激活您的virtualenv(这很重要,因为它还将在当前virtualenv中安装新版本的升级软件包)。

cd 进入您的项目目录,然后运行:

pip-upgrade

高级用法

如果需求放置在非标准位置,请将其作为参数发送:

pip-upgrade path/to/requirements.txt

如果您已经知道要升级的软件包,只需将它们作为参数发送:

pip-upgrade -p django -p celery -p dateutil

如果您需要升级到发行前/发行后版本,--prerelease请在命令中添加参数。

全面披露:我写了这个包裹。

I had the same problem with upgrading. Thing is, i never upgrade all packages. I upgrade only what i need, because project may break.

Because there was no easy way for upgrading package by package, and updating the requirements.txt file, i wrote this pip-upgrader which also updates the versions in your requirements.txt file for the packages chosen (or all packages).

Installation

pip install pip-upgrader

Usage

Activate your virtualenv (important, because it will also install the new versions of upgraded packages in current virtualenv).

cd into your project directory, then run:

pip-upgrade

Advanced usage

If the requirements are placed in a non-standard location, send them as arguments:

pip-upgrade path/to/requirements.txt

If you already know what package you want to upgrade, simply send them as arguments:

pip-upgrade -p django -p celery -p dateutil

If you need to upgrade to pre-release / post-release version, add --prerelease argument to your command.

Full disclosure: I wrote this package.


回答 12

https://github.com/cakebread/yolk

$ pip install -U `yolk -U | awk '{print $1}' | uniq`

但是,您需要先获得蛋黄:

$ sudo pip install -U yolk

From https://github.com/cakebread/yolk :

$ pip install -U `yolk -U | awk '{print $1}' | uniq`

however you need to get yolk first:

$ sudo pip install -U yolk

回答 13

@Ramana的答案的一线版。

python -c 'import pip, subprocess; [subprocess.call("pip install -U " + d.project_name, shell=1) for d in pip.get_installed_distributions()]'

`

One-liner version of @Ramana’s answer.

python -c 'import pip, subprocess; [subprocess.call("pip install -U " + d.project_name, shell=1) for d in pip.get_installed_distributions()]'

`


回答 14

使用virtualenv时,如果您只想升级添加到virtualenv中的软件包,则可能需要执行以下操作:

pip install `pip freeze -l | cut --fields=1 -d = -` --upgrade

when using a virtualenv and if you just want to upgrade packages added to your virtualenv, you may want to do:

pip install `pip freeze -l | cut --fields=1 -d = -` --upgrade

回答 15

点子问题讨论中找到的最简单,最快的解决方案是:

pip install pipdate
pipdate

来源:https//github.com/pypa/pip/issues/3819

The simplest and fastest solution that I found in the pip issue discussion is:

pip install pipdate
pipdate

Source: https://github.com/pypa/pip/issues/3819


回答 16

Windows Powershell解决方案

pip freeze | %{$_.split('==')[0]} | %{pip install --upgrade $_}

Windows Powershell solution

pip freeze | %{$_.split('==')[0]} | %{pip install --upgrade $_}

回答 17

使用awk更新包: pip install -U $(pip freeze | awk -F'[=]' '{print $1}')

Windows Powershell更新 foreach($p in $(pip freeze)){ pip install -U $p.Split("=")[0]}

use awk update packges: pip install -U $(pip freeze | awk -F'[=]' '{print $1}')

windows powershell update foreach($p in $(pip freeze)){ pip install -U $p.Split("=")[0]}


回答 18

您可以尝试以下方法:

for i in ` pip list|awk -F ' ' '{print $1}'`;do pip install --upgrade $i;done

You can try this :

for i in ` pip list|awk -F ' ' '{print $1}'`;do pip install --upgrade $i;done

回答 19

相当惊人的蛋黄使这一过程变得容易。

pip install yolk3k # don't install `yolk`, see https://github.com/cakebread/yolk/issues/35
yolk --upgrade

有关蛋黄的更多信息:https : //pypi.python.org/pypi/yolk/0.4.3

它可以做很多您可能会发现有用的事情。

The rather amazing yolk makes this easy.

pip install yolk3k # don't install `yolk`, see https://github.com/cakebread/yolk/issues/35
yolk --upgrade

For more info on yolk: https://pypi.python.org/pypi/yolk/0.4.3

It can do lots of things you’ll probably find useful.


回答 20

@Ramana的答案对我来说最有效,但是我不得不添加一些注意事项:

import pip
for dist in pip.get_installed_distributions():
    if 'site-packages' in dist.location:
        try:
            pip.call_subprocess(['pip', 'install', '-U', dist.key])
        except Exception, exc:
            print exc

site-packages检查不包括我的开发包,因为它们不在系统site-packages目录中。try-except仅跳过已从PyPI中删除的软件包。

@endolith:我也希望有一个简单的方法pip.install(dist.key, upgrade=True),但是它看起来不像pip应该被命令行以外的任何东西使用(文档没有提到内部API,并且pip开发人员没有使用文档字符串)。

@Ramana’s answer worked the best for me, of those here, but I had to add a few catches:

import pip
for dist in pip.get_installed_distributions():
    if 'site-packages' in dist.location:
        try:
            pip.call_subprocess(['pip', 'install', '-U', dist.key])
        except Exception, exc:
            print exc

The site-packages check excludes my development packages, because they are not located in the system site-packages directory. The try-except simply skips packages that have been removed from PyPI.

@endolith: I was hoping for an easy pip.install(dist.key, upgrade=True), too, but it doesn’t look like pip was meant to be used by anything but the command line (the docs don’t mention the internal API, and the pip developers didn’t use docstrings).


回答 21

pip_upgrade_outdated做这项工作。根据其文档

usage: pip_upgrade_outdated [-h] [-3 | -2 | --pip_cmd PIP_CMD]
                            [--serial | --parallel] [--dry_run] [--verbose]
                            [--version]

Upgrade outdated python packages with pip.

optional arguments:
  -h, --help         show this help message and exit
  -3                 use pip3
  -2                 use pip2
  --pip_cmd PIP_CMD  use PIP_CMD (default pip)
  --serial, -s       upgrade in serial (default)
  --parallel, -p     upgrade in parallel
  --dry_run, -n      get list, but don't upgrade
  --verbose, -v      may be specified multiple times
  --version          show program's version number and exit

步骤1:

pip install pip-upgrade-outdated

第2步:

pip_upgrade_outdated

The pip_upgrade_outdated does the job. According to its docs:

usage: pip_upgrade_outdated [-h] [-3 | -2 | --pip_cmd PIP_CMD]
                            [--serial | --parallel] [--dry_run] [--verbose]
                            [--version]

Upgrade outdated python packages with pip.

optional arguments:
  -h, --help         show this help message and exit
  -3                 use pip3
  -2                 use pip2
  --pip_cmd PIP_CMD  use PIP_CMD (default pip)
  --serial, -s       upgrade in serial (default)
  --parallel, -p     upgrade in parallel
  --dry_run, -n      get list, but don't upgrade
  --verbose, -v      may be specified multiple times
  --version          show program's version number and exit

Step 1:

pip install pip-upgrade-outdated

Step 2:

pip_upgrade_outdated

回答 22

通过拉动请求发送给小学生。同时使用此pip库解决方案,我写道:

from pip import get_installed_distributions
from pip.commands import install

install_cmd = install.InstallCommand()

options, args = install_cmd.parse_args([package.project_name
                                        for package in
                                        get_installed_distributions()])

options.upgrade = True
install_cmd.run(options, args)  # Chuck this in a try/except and print as wanted

Sent through a pull-request to the pip folk; in the meantime use this pip library solution I wrote:

from pip import get_installed_distributions
from pip.commands import install

install_cmd = install.InstallCommand()

options, args = install_cmd.parse_args([package.project_name
                                        for package in
                                        get_installed_distributions()])

options.upgrade = True
install_cmd.run(options, args)  # Chuck this in a try/except and print as wanted

回答 23

这似乎对我有用…

pip install -U $(pip list --outdated|awk '{printf $1" "}')

printf之后,我使用了一个空格来正确分隔软件包名称。

This seemed to work for me…

pip install -U $(pip list --outdated|awk '{printf $1" "}')

I used printf with a space afterwards to properly separate the package names.


回答 24

这是针对Python 3的PowerShell解决方案:

pip3 list --outdated --format=legacy | ForEach { pip3 install -U $_.split(" ")[0] }

对于Python 2:

pip2 list --outdated --format=legacy | ForEach { pip2 install -U $_.split(" ")[0] }

这将一个接一个地升级软件包。所以

pip3 check
pip2 check

之后应确保没有依赖项被破坏。

This is a PowerShell solution for Python 3:

pip3 list --outdated --format=legacy | ForEach { pip3 install -U $_.split(" ")[0] }

And for Python 2:

pip2 list --outdated --format=legacy | ForEach { pip2 install -U $_.split(" ")[0] }

This upgrades the packages one by one. So a

pip3 check
pip2 check

afterwards should make sure no dependencies are broken.


回答 25

怎么样:

pip install -r <(pip freeze) --upgrade

How about:

pip install -r <(pip freeze) --upgrade

回答 26

在Windows上最短,最简单。

pip freeze > requirements.txt && pip install --upgrade -r requirements.txt && rm requirements.txt

The shortest and easiest on Windows.

pip freeze > requirements.txt && pip install --upgrade -r requirements.txt && rm requirements.txt

回答 27

我的剧本:

pip list --outdated --format=legacy | cut -d ' ' -f1 | xargs -n1 pip install --upgrade

My script:

pip list --outdated --format=legacy | cut -d ' ' -f1 | xargs -n1 pip install --upgrade

回答 28

这不是更有效吗?

pip3 list -o | grep -v -i warning | cut -f1 -d' ' | tr " " "\n" | awk '{if(NR>=3)print}' | cut -d' ' -f1 | xargs -n1 pip3 install -U 
  1. pip list -o 列出过期的软件包;
  2. grep -v -i warning启用反向匹配warning以避免更新时出错
  3. cut -f1 -d1' ' 返回第一个单词-过时的包的名称;
  4. tr "\n|\r" " "将多行结果cut转换为单行,以空格分隔的列表;
  5. awk '{if(NR>=3)print}' 跳过标题行
  6. cut -d' ' -f1 获取第一列
  7. xargs -n1 pip install -U 从左管道中获取1个参数,并将其传递给命令以升级软件包列表。

Isn’t this more effective?

pip3 list -o | grep -v -i warning | cut -f1 -d' ' | tr " " "\n" | awk '{if(NR>=3)print}' | cut -d' ' -f1 | xargs -n1 pip3 install -U 
  1. pip list -o lists outdated packages;
  2. grep -v -i warning inverted match on warning to avoid errors when updating
  3. cut -f1 -d1' ' returns the first word – the name of the outdated package;
  4. tr "\n|\r" " " converts the multiline result from cut into a single-line, space-separated list;
  5. awk '{if(NR>=3)print}' skips header lines
  6. cut -d' ' -f1 fetches the first column
  7. xargs -n1 pip install -U takes 1 argument from the pipe left of it, and passes it to the command to upgrade the list of packages.

回答 29

在Powershell 5.1中具有adm权限,python 3.6.5和pip ver 10.0.1的一行:

pip list -o --format json | ConvertFrom-Json | foreach {pip install $_.name -U --no-warn-script-location}

如果列表中没有破损的包装或特殊的轮子,它会正常工作…

one line in powershell 5.1 with adm rights, python 3.6.5 and pip ver 10.0.1:

pip list -o --format json | ConvertFrom-Json | foreach {pip install $_.name -U --no-warn-script-location}

it works smoothly if there are no broken packages or special wheels in the list…


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