是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
当前回答
JSON+jq答案:
pip list -o --format json | jq '.[] | .name' | xargs pip install -U
其他回答
我在pip问题讨论中找到的最简单、最快的解决方案是:
pip install pipdate
pipdate
资料来源:https://github.com/pypa/pip/issues/3819
以下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
这里发布的所有解决方案都打破了依赖关系。
在此对话中,将功能直接包含到pip中,包括正确管理依赖关系:
的作者https://github.com/kdeldycke/meta-package-manager写入MPM可以模拟缺少的upgrade all命令:
mpm --pip upgrade --all
Use:
pip install -r <(pip freeze) --upgrade
一行(bash)。对我来说最短、最简单。
pip install -U $(pip freeze | cut -d = -f 1)
解释:
pip冻结返回每个包的package_name==版本cut-d=-f 1表示“对于每一行,返回第一行的字段,其中字段由=分隔”$(cmd)返回命令cmd的结果。因此,在这里,cmd将返回包名列表,pip install-U将对其进行升级。