2461

で一度にすべての Python パッケージをアップグレードすることは可能pipですか?

:公式のイシュー トラッカーには、この機能に関するリクエストがあります。

4

52 に答える 52

2627

組み込みのフラグはまだありませんが、使用できます

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

@jawachegrepで提案されているように、編集可能な (「-e」) パッケージ定義をスキップすることです。grep(はい、 +cutsedorawkまたはor...に置き換えることができperlます)。

-n1フラグは、xargs1 つのパッケージの更新が失敗した場合にすべてを停止することを防ぎます ( @andsens に感謝します)。

于 2010-08-10T19:56:49.130 に答える
776

次の 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)
于 2011-04-30T03:31:16.610 に答える
146

Rob van der Woude による優れたドキュメントをFOR参照した後の Windows バージョン:

for /F "delims===" %i in ('pip freeze') do pip install --upgrade %i
于 2012-02-25T18:04:34.960 に答える
75

古くなったパッケージを印刷するだけです:

pip freeze | cut -d = -f 1 | xargs -n 1 pip search | grep -B2 'LATEST:'
于 2011-06-10T12:50:49.957 に答える
43

これはより簡潔に思えます。

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

-n1pip install -U各コマンドに渡される引数の数を 1 に制限します

于 2016-06-10T03:47:23.097 に答える
29

https://github.com/cakebread/yolkから:

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

ただし、最初に卵黄を取得する必要があります。

$ sudo pip install -U yolk
于 2012-04-03T21:38:18.050 に答える
25

virtualenvを使用していて、 virtualenv に追加されたパッケージをアップグレードするだけの場合は、次のようにすることができます。

pip install `pip freeze -l | cut --fields=1 -d = -` --upgrade
于 2011-09-13T09:42:32.737 に答える
21

Windows PowerShell ソリューション

pip freeze | %{$_.split('==')[0]} | %{pip install --upgrade $_}
于 2016-09-16T09:07:18.190 に答える
13

管理者権限、Python 3.6.5、およびpipバージョン 10.0.1を使用するPowerShell 5.1の 1 行:

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

リストに壊れたパッケージや特別なホイールがなければ、スムーズに動作します...

于 2018-06-25T11:56:27.557 に答える
12

これを試すことができます:

for i in ` pip list | awk -F ' ' '{print $1}'`; do pip install --upgrade $i; done
于 2013-07-17T01:43:47.033 に答える
10

ラマナの答えは、ここにあるものの中で私にとって最もうまくいきましたが、いくつかのキャッチを追加する必要がありました:

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開発者は使用していませんdocstrings)。

于 2012-10-27T22:56:07.633 に答える
9

これは私にとってはうまくいくようでした...

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

printfパッケージ名を適切に区切るために、後でスペースを使用しました。

于 2015-08-05T22:20:06.577 に答える
9

プルリクエストを通じてpip の人々に送信されました。それまでの間、私が書いたこの 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
于 2014-01-26T09:31:33.483 に答える
8

これはより効果的であるはずです:

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 つの引数を取り、それをコマンドに渡してパッケージのリストをアップグレードします。
于 2014-10-09T14:23:56.493 に答える
5

古いパッケージのみを更新するスクリプトを次に示します。

import os, sys
from subprocess import check_output, call

file = check_output(["pip.exe",  "list", "--outdated", "--format=legacy"])
line = str(file).split()

for distro in line[::6]:
    call("pip install --upgrade " + distro, shell=True)

レガシー形式 (バージョン 18 以降) として出力されない新しいバージョンの pip の場合:

import os, sys
from subprocess import check_output, call

file = check_output(["pip.exe", "list", "-o", "--format=json"])
line = str(file).split()

for distro in line[1::8]:
    distro = str(distro).strip('"\",')
    call("pip install --upgrade " + distro, shell=True)
于 2016-11-04T11:34:25.183 に答える
5

これは、「編集可能な」ディストリビューションと開発ディストリビューションをバイパスするrbp's answer の私のバリエーションです。オリジナルと同じ 2 つの欠点があります。不必要に再ダウンロードして再インストールします。1 つのパッケージでエラーが発生すると、その後のすべてのパッケージのアップグレードが妨げられます。

pip freeze |sed -ne 's/==.*//p' |xargs pip install -U --

関連するバグ レポート。Bitbucket からの移行後に少しバラバラになりました。

于 2011-05-24T10:58:20.297 に答える
5

古いパッケージをすべて見る

 pip list --outdated --format=columns

インストール

 sudo pip install pipdate

次に入力します

 sudo -H pipdate
于 2018-07-19T06:41:58.663 に答える
5

以下の Windowscmdスニペットは、次のことを行います。

  • pip最新バージョンにアップグレードします。
  • すべての古いパッケージをアップグレードします。
  • requirements.txtアップグレードされるパッケージごとに、バージョン指定子がチェックされます。
@echo off
Setlocal EnableDelayedExpansion
rem https://stackoverflow.com/questions/2720014/

echo Upgrading pip...
python -m pip install --upgrade pip
echo.

echo Upgrading packages...
set upgrade_count=0
pip list --outdated > pip-upgrade-outdated.txt
for /F "skip=2 tokens=1,3 delims= " %%i in (pip-upgrade-outdated.txt) do (
    echo ^>%%i
    set package=%%i
    set latest=%%j
    set requirements=!package!

    rem for each outdated package check for any version requirements:
    set dotest=1
    for /F %%r in (.\python\requirements.txt) do (
        if !dotest!==1 (
            call :substr "%%r" !package! _substr
            rem check if a given line refers to a package we are about to upgrade:
            if "%%r" NEQ !_substr! (
                rem check if the line contains more than just a package name:
                if "%%r" NEQ "!package!" (
                    rem set requirements to the contents of the line:
                    echo requirements: %%r, latest: !latest!
                    set requirements=%%r
                )
                rem stop testing after the first instance found,
                rem prevents from mistakenly matching "py" with "pylint", "numpy" etc.
                rem requirements.txt must be structured with shorter names going first
                set dotest=0
            )
        )
    )
    rem pip install !requirements!
    pip install --upgrade !requirements!
    set /a "upgrade_count+=1"
    echo.
)

if !upgrade_count!==0 (
    echo All packages are up to date.
) else (
    type pip-upgrade-outdated.txt
)

if "%1" neq "-silent" (
    echo.
    set /p temp="> Press Enter to exit..."
)
exit /b


:substr
rem string substition done in a separate subroutine -
rem allows expand both variables in the substring syntax.
rem replaces str_search with an empty string.
rem returns the result in the 3rd parameter, passed by reference from the caller.
set str_source=%1
set str_search=%2
set str_result=!str_source:%str_search%=!
set "%~3=!str_result!"
rem echo !str_source!, !str_search!, !str_result!
exit /b
于 2019-08-10T02:59:39.473 に答える
4

Ramana のコードを試してみたところ、Ubuntu ではsudoコマンドごとに記述する必要があることがわかりました。Ubuntu 13.10(Saucy Salamander)で正常に動作するスクリプトは次のとおりです。

#!/usr/bin/env python
import pip
from subprocess import call

for dist in pip.get_installed_distributions():
    call("sudo pip install --upgrade " + dist.project_name, shell=True)
于 2014-03-03T08:32:00.833 に答える
4

Python でスクリプトを使用する別の方法を次に示します。

import pip, tempfile, contextlib

with tempfile.TemporaryFile('w+') as temp:
    with contextlib.redirect_stdout(temp):
        pip.main(['list', '-o'])
    temp.seek(0)
    for line in temp:
        pk = line.split()[0]
        print('--> updating', pk, '<--')
        pip.main(['install', '-U', pk])
于 2016-04-11T23:07:42.583 に答える