是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
当前回答
在Windows或Linux上更新Python包
1-将已安装软件包的列表输出到需求文件(requirements.txt)中:
pip freeze > requirements.txt
2-编辑requirements.txt,并将所有“==”替换为“>=”。在编辑器中使用“全部替换”命令。
3-升级所有过时的软件包
pip install -r requirements.txt --upgrade
资料来源:https://www.activestate.com/resources/quick-reads/how-to-update-all-python-packages/
其他回答
import os
import pip
from subprocess import call, check_call
pip_check_list = ['pip', 'pip3']
pip_list = []
FNULL = open(os.devnull, 'w')
for s_pip in pip_check_list:
try:
check_call([s_pip, '-h'], stdout=FNULL)
pip_list.append(s_pip)
except FileNotFoundError:
pass
for dist in pip.get_installed_distributions():
for pip in pip_list:
call("{0} install --upgrade ".format(pip) + dist.project_name, shell=True)
我接受了拉玛纳的回答,并使其变得友好。
在我看来,这个选项更简单易懂:
pip install -U `pip list --outdated | awk 'NR>2 {print $1}'`
解释是,pip-list——过时输出所有过时包的列表,格式如下:
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确实可以处理跳过记录)。
以下Windows cmd代码段执行以下操作:
将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
没有必要这么麻烦或安装一些软件包。
在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或pip2。pip-list——过时,用于检查过时的pip包。--我的pip版本22.0.3的格式只有3种类型:列(默认)、冻结或json。冻结是命令管道中更好的选项。尽可能多地保持命令简单和可用。
JSON+jq答案:
pip list -o --format json | jq '.[] | .name' | xargs pip install -U