で一度にすべての Python パッケージをアップグレードすることは可能pip
ですか?
注:公式のイシュー トラッカーには、この機能に関するリクエストがあります。
組み込みのフラグはまだありませんが、使用できます
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
(はい、 +cut
をsed
orawk
またはor...に置き換えることができperl
ます)。
の-n1
フラグは、xargs
1 つのパッケージの更新が失敗した場合にすべてを停止することを防ぎます ( @andsens に感謝します)。
次の 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)
Rob van der Woude による優れたドキュメントをFOR
参照した後の Windows バージョン:
for /F "delims===" %i in ('pip freeze') do pip install --upgrade %i
古くなったパッケージを印刷するだけです:
pip freeze | cut -d = -f 1 | xargs -n 1 pip search | grep -B2 'LATEST:'
これはより簡潔に思えます。
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 に制限します
https://github.com/cakebread/yolkから:
$ pip install -U `yolk -U | awk '{print $1}' | uniq`
ただし、最初に卵黄を取得する必要があります。
$ sudo pip install -U yolk
virtualenvを使用していて、 virtualenv に追加されたパッケージをアップグレードするだけの場合は、次のようにすることができます。
pip install `pip freeze -l | cut --fields=1 -d = -` --upgrade
pip freeze | %{$_.split('==')[0]} | %{pip install --upgrade $_}
管理者権限、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}
リストに壊れたパッケージや特別なホイールがなければ、スムーズに動作します...
これを試すことができます:
for i in ` pip list | awk -F ' ' '{print $1}'`; do pip install --upgrade $i; done
ラマナの答えは、ここにあるものの中で私にとって最もうまくいきましたが、いくつかのキャッチを追加する必要がありました:
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)。
これは私にとってはうまくいくようでした...
pip install -U $(pip list --outdated | awk '{printf $1" "}')
printf
パッケージ名を適切に区切るために、後でスペースを使用しました。
プルリクエストを通じて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
これはより効果的であるはずです:
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
pip list -o
古いパッケージを一覧表示します。grep -v -i warning
warning
更新時のエラーを回避するために反転一致をオンにしますcut -f1 -d1' '
最初の単語 (古いパッケージの名前) を返します。tr "\n|\r" " "
からの複数行の結果をcut
スペースで区切られた単一行のリストに変換します。awk '{if(NR>=3)print}'
ヘッダー行をスキップしますcut -d' ' -f1
最初の列をフェッチしますxargs -n1 pip install -U
その左のパイプから 1 つの引数を取り、それをコマンドに渡してパッケージのリストをアップグレードします。古いパッケージのみを更新するスクリプトを次に示します。
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)
これは、「編集可能な」ディストリビューションと開発ディストリビューションをバイパスするrbp's answer の私のバリエーションです。オリジナルと同じ 2 つの欠点があります。不必要に再ダウンロードして再インストールします。1 つのパッケージでエラーが発生すると、その後のすべてのパッケージのアップグレードが妨げられます。
pip freeze |sed -ne 's/==.*//p' |xargs pip install -U --
関連するバグ レポート。Bitbucket からの移行後に少しバラバラになりました。
古いパッケージをすべて見る
pip list --outdated --format=columns
インストール
sudo pip install pipdate
次に入力します
sudo -H pipdate
以下の 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
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)
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])