是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
当前回答
改用pipx:
pipx upgrade-all
其他回答
相当惊人的蛋黄使这很容易。
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
它可以做很多你可能会发现有用的事情。
这似乎对我有用。。。
pip install -U $(pip list --outdated | awk '{printf $1" "}')
之后,我使用带有空格的printf来正确分隔包名。
使用pip升级程序
使用该库,您可以轻松升级所有依赖项包这些是您遵循的设置。
pip安装pip升级程序
pip升级路径/of/requirements_txt_file
交互式pip需求升级程序。因为逐包升级需求是件麻烦事。它还会更新requirements.txt文件中的版本。
以下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
以下命令在Windows上运行,对其他人也很有用($是命令提示符下的任何目录。例如,C:\Users/Username)。
Do
$ pip freeze > requirements.txt
打开文本文件,将==替换为>=,或让sed为您执行以下操作:
$ sed -i 's/==/>=/g' requirements.txt
并执行:
$ pip install -r requirements.txt --upgrade
如果您对某个软件包延迟升级有问题(有时是NumPy),只需转到目录($),注释掉名称(在其前面添加#),然后再次运行升级。稍后可以取消注释该部分。这对于复制Python全局环境也非常有用。
另一种方式:
我也喜欢pip审查方法:
py2
$ pip install pip-review
$ pip-review --local --interactive
py3
$ pip3 install pip-review
$ py -3 -m pip-review --local --interactive
您可以选择“a”来升级所有软件包;如果一次升级失败,请再次运行,然后在下一次升级中继续。