如何使用 pip 升级所有 Python 包

发布于 2024-08-30 09:04:29 字数 315 浏览 6 评论 0原文

是否可以使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

澜川若宁 2024-09-06 09:04:29

目前还没有内置标志。从 pip 版本 22.3 开始,--outdated--format=freeze 已变为 相互排斥。使用 Python 解析 JSON 输出:

pip --disable-pip-version-check list --outdated --format=json | python -c "import json, sys; print('\n'.join([x['name'] for x in json.load(sys.stdin)]))" | xargs -n1 pip install -U

如果您使用的是 pip<22.3,您可以使用:

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 是跳过可编辑(“-e”)包定义,如 @jawache。 (是的,您可以将 grep+cut 替换为 sedawkperl或...)。


  • xargs-n1 标志可防止在更新某个包失败时停止所有操作(感谢 @andsens)。


注意:这有无限的潜在变化。我试图让这个答案简短而简单,但请在评论中提出建议!

There isn't a built-in flag yet. Starting with pip version 22.3, the --outdated and --format=freeze have become mutually exclusive. Use Python, to parse the JSON output:

pip --disable-pip-version-check list --outdated --format=json | python -c "import json, sys; print('\n'.join([x['name'] for x in json.load(sys.stdin)]))" | xargs -n1 pip install -U

If you are using pip<22.3 you can use:

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

For older versions of pip:

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).


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!

带刺的爱情 2024-09-06 09:04:29

要升级所有本地软件包,您可以安装 pip-review

$ pip install pip-review

之后您可以以交互方式升级软件包:

$ pip-review --local --interactive

或者自动升级:

$ pip-review --local --auto

pip-reviewpip-tools 的分支。请参阅 pip-tools 问题 提到的="https://stackoverflow.com/questions/2720014/upgrading-all-packages-with-pip/16269635#comment51585726_16269635">@knedlsepp。 pip-review 包可以工作,但 pip-tools 包不再工作。 pip-review 正在寻找新的维护者。

pip-review 适用于 Windows 自版本 0.5。

To upgrade all local packages, you can install pip-review:

$ pip install pip-review

After that, you can either upgrade the packages interactively:

$ pip-review --local --interactive

Or automatically:

$ pip-review --local --auto

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 is looking for a new maintainer.

pip-review works on Windows since version 0.5.

你不是我要的菜∠ 2024-09-06 09:04:29

您可以使用以下 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)
稚气少女 2024-09-06 09:04:29

以下内容适用于 Windows,也应该对其他人有好处($ 是命令提示符中您所在的任何目录。例如,C:/Users/Username )。

打开

$ pip freeze > requirements.txt

文本文件,将 == 替换为 >=,或者让 sed 为您执行:

$ sed -i 's/==/>=/g' 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”来升级所有软件包;如果一次升级失败,请再次运行,然后继续进行下一次升级。

The following works on Windows and should be good for others too ($ is whatever directory you're in, in the command prompt. For example, C:/Users/Username).

Do

$ pip freeze > requirements.txt

Open the text file, replace the == with >=, or have sed do it for you:

$ sed -i 's/==/>=/g' requirements.txt

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.

这个俗人 2024-09-06 09:04:29

使用pipupgrade! ...最新版本 2019

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

pipupgrade 可帮助您从 requirements.txt 文件升级系统、本地或软件包!它还选择性地升级不会破坏更改的软件包。

pipupgrade 还确保升级多个 Python 环境中存在的包。它与 Python 2.7+、Python 3.4+ 和 pip 9+、pip 10+、pip 18+、pip 19+ 兼容。

输入图像描述这里

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

Use pipupgrade! ... last release 2019

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. It is compatible with Python 2.7+, Python 3.4+ and pip 9+, pip 10+, pip 18+, pip 19+.

Enter image description here

Note: I'm the author of the tool.

汐鸠 2024-09-06 09:04:29

Windows 版本参考了 Rob 的优秀 FOR 文档van der Woude:

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

您需要使用cmd.exe(即命令提示符);现在,Windows 默认使用 PowerShell(特别是如果您使用 Windows 终端),该命令不能直接使用该命令。或者在 PowerShell 中输入 cmd 以访问命令提示符。

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

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

You need to use cmd.exe (i.e, Command Prompt); Windows nowadays defaults to PowerShell (especially if you use Windows Terminal) for which this command doesn't work directly. Or type cmd at PowerShell to access Command Prompt.

魂牵梦绕锁你心扉 2024-09-06 09:04:29

在我看来,这个选项更简单易读:

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).

む无字情书 2024-09-06 09:04:29

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

(pip >= 22.3)

根据这个可读的答案

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

或根据接受的答案

pip --disable-pip-version-check list --outdated --format=json |
    python -c "import json, sys; print('\n'.join([x['name'] for x in json.load(sys.stdin)]))" |
    xargs -n1 pip install -U

(pip 20.0 < 22.3)

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

旧版本:

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

如果发生错误,xargs -n1 会继续运行。

如果您需要对省略的内容和引发错误的内容进行更多“细粒度”控制,则不应添加 -n1 标志并通过为每个内容“管道”以下行来显式定义要忽略的错误单独的错误:

<代码> | sed 's/^<错误的第一个字符>.*//'

这是一个工作示例:

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 >= 22.3)

as per this readable answer:

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

or as per the accepted answer:

pip --disable-pip-version-check list --outdated --format=json |
    python -c "import json, sys; print('\n'.join([x['name'] for x in json.load(sys.stdin)]))" |
    xargs -n1 pip install -U

(pip 20.0 < 22.3)

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
雨落□心尘 2024-09-06 09:04:29

您可以只打印过时的包:

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:'
海夕 2024-09-06 09:04:29

更强大的解决方案

对于pip3,请使用:

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

对于pip,只需删除 3,如下所示:

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

OS X Oddity

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

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

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

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

上述命令使用最简单且最可移植的 pip 语法与 sedsh 结合使用可以完全克服这些问题。可以使用注释版本2仔细检查sed操作的详细信息。


详细信息

[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

OS X Oddity

OS X, 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 of 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 the 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.

手心的海 2024-09-06 09:04:29

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

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

安装

pip install pip-upgrader

使用

激活你的 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.

秋心╮凉 2024-09-06 09:04:29

这样看起来更简洁。

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]

In cut -d ' ' -f1, -d ' ' set "space" as分隔符,-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.

不交电费瞎发啥光 2024-09-06 09:04:29

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()]'
画中仙 2024-09-06 09:04:29

来自蛋黄

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

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

sudo pip install -U yolk

From yolk:

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

However, you need to get yolk first:

sudo pip install -U yolk
违心° 2024-09-06 09:04:29

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 (based on this older script) does the job. According to its documentation:

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
南城追梦 2024-09-06 09:04:29

在 Windows 或 Linux 上更新 Python 包

  1. 将已安装包的列表输出到需求文件 (requirements.txt) 中:

    点冻结>要求.txt
    
  2. 编辑requirements.txt,并将所有“==”替换为“>=”。使用编辑器中的“全部替换”命令。

  3. 升级所有过时的软件包

    pip install -rrequirements.txt --upgrade
    

来源:如何更新所有Python包

Updating Python packages on Windows or Linux

  1. Output a list of installed packages into a requirements file (requirements.txt):

    pip freeze > requirements.txt
    
  2. Edit requirements.txt, and replace all ‘==’ with ‘>=’. Use the ‘Replace All’ command in the editor.

  3. Upgrade all outdated packages

    pip install -r requirements.txt --upgrade
    

Source: How to Update All Python Packages

笑梦风尘 2024-09-06 09:04:29

我在 pip 问题讨论中找到的最简单、最快的解决方案是:

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

泡沫很甜 2024-09-06 09:04:29

当使用 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
鹿港小镇 2024-09-06 09:04:29

Windows PowerShell 解决方案

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

Windows PowerShell solution

pip freeze | %{$_.split('==')[0]} | %{pip install --upgrade $_}
顾挽 2024-09-06 09:04:29

使用 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 packages:

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

Windows PowerShell update

foreach($p in $(pip freeze)){ pip install -U $p.Split("=")[0]}
流心雨 2024-09-06 09:04:29

PowerShell 5.1(具有管理员权限)、Python 3.6.5 和 pip 中的一行 /em> 版本 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 administrator rights, Python 3.6.5, and pip version 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...

山川志 2024-09-06 09:04:29

你可以试试这个:

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
梦冥 2024-09-06 09:04:29

如果您安装了 pip<22.3,则纯 Bash/Z shell 用于实现这一目标的单行代码:

for p in $(pip list -o --format freeze); do pip install -U ${p%%=*}; done

或者,以一种格式良好的方式:

for p in $(pip list -o --format freeze)
do
    pip install -U ${p%%=*}
done

之后你将得到 pip>=22.3,其中 -o--format freeze 是互斥的,并且你不能再使用这一句。

If you have pip<22.3 installed, a pure Bash/Z shell one-liner for achieving that:

for p in $(pip list -o --format freeze); do pip install -U ${p%%=*}; done

Or, in a nicely-formatted way:

for p in $(pip list -o --format freeze)
do
    pip install -U ${p%%=*}
done

After this you will have pip>=22.3 in which -o and --format freeze are mutually exclusive, and you can no longer use this one-liner.

一袭白衣梦中忆 2024-09-06 09:04:29

相当神奇的蛋黄使这一切变得容易。

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 information on yolk: https://pypi.python.org/pypi/yolk/0.4.3

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

陪你到最终 2024-09-06 09:04:29

使用:

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

Use:

pip install -r <(pip freeze) --upgrade
み零 2024-09-06 09:04:29

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
三人与歌 2024-09-06 09:04:29

没必要那么麻烦或者安装一些包。

更新 Linux shell 上的 pip 软件包:

pip list --outdated --format=freeze | awk -F"==" '{print $1}' | xargs -i pip install -U {}

更新 Windows powershell 上的 pip 软件包:

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

几点:

  • pip 作为您的 python 版本替换为 pip3< /code> 或 pip2
  • pip list --outdated 用于检查过时的 pip 包。
  • 我的 pip 版本 22.0.3 上的 --format 只有 3 种类型:columns(默认)、freeze 或 jsonfreeze 是命令管道中更好的选项。
  • 使命令简单且可用尽可能多的系统。

There is not necessary to be so troublesome or install some package.

Update pip packages on Linux shell:

pip list --outdated --format=freeze | awk -F"==" '{print $1}' | xargs -i pip install -U {}

Update pip packages on Windows powershell:

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

Some points:

  • Replace pip as your python version to pip3 or pip2.
  • pip list --outdated to check outdated pip packages.
  • --format on my pip version 22.0.3 only has 3 types: columns (default), freeze, or json. freeze is better option in command pipes.
  • Keep command simple and usable as many systems as possible.
淡看悲欢离合 2024-09-06 09:04:29

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 中删除的包。

到内膜:我希望有一个简单的< code>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.

To 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).

长亭外,古道边 2024-09-06 09:04:29

这是针对 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.

贪恋 2024-09-06 09:04:29

这应该更有效:

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 warningwarning 进行反向匹配,以避免更新
  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 个参数,并将其传递给命令以升级包列表。

This ought to be 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文