rbp*_*rbp 2051
还没有内置标志,但您可以使用
pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U
Run Code Online (Sandbox Code Playgroud)
注意:这有无限的潜在变化.我试图保持这个简短而简单的答案,但请在评论中建议变化!
在旧版本中pip
,您可以使用此代码:
pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U
Run Code Online (Sandbox Code Playgroud)
的grep
是跳过编辑("-e")包的定义,通过@jawache的建议.(是的,你可以取代grep
+ cut
与sed
或awk
或perl
或......).
如果更新一个包失败-n1
,则xargs
阻止停止所有内容的标志(感谢@andsens).
Ram*_*ana 636
您可以使用以下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)
Run Code Online (Sandbox Code Playgroud)
对于pip> = 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)
Run Code Online (Sandbox Code Playgroud)
jfs*_*jfs 610
升级所有本地包; 你可以使用pip-review
:
$ pip install pip-review
$ pip-review --local --interactive
Run Code Online (Sandbox Code Playgroud)
pip-review
是一个分叉pip-tools
.见pip-tools
问题被提到@knedlsepp.pip-review
包工作但pip-tools
包不再有效.
pip-review
自0.5版以来在Windows上运行.
aza*_*aks 280
适用于Windows.也应该对别人有好处.($是你所在的目录,在命令提示符下.例如C:/ Users/Username>)
做
$ pip freeze > requirements.txt
Run Code Online (Sandbox Code Playgroud)
打开文本文件,用= =替换==
然后做
$ pip install -r requirements.txt --upgrade
Run Code Online (Sandbox Code Playgroud)
如果某个程序包出现问题而导致升级失败(有时会出现问题),只需转到目录($),注释掉名称(在它之前添加#)并再次运行升级.您可以稍后取消注释该部分.这对于复制python全局环境也很有用.
我也喜欢pip-review方法:
py2
$ pip install pip-review
$ pip-review --local --interactive
py3
$ pip3 install pip-review
$ py -3 -m pip_review --local --interactive
Run Code Online (Sandbox Code Playgroud)
您可以选择"a"来升级所有包; 如果一次升级失败,请再次运行并继续下一次升级.
Pio*_*ost 118
咨询优良的售后服务Windows版本文档的FOR
罗布范德Woude
for /F "delims===" %i in ('pip freeze -l') do pip install -U %i
Run Code Online (Sandbox Code Playgroud)
Ach*_*nha 85
$ pip install pipupgrade
$ pipupgrade --latest --yes
Run Code Online (Sandbox Code Playgroud)
pipupgrade帮助您从requirements.txt
文件升级系统,本地或包!它还有选择地升级不会破坏变更的软件包.兼容Python2.7 +,Python3.4 +和pip9 +,pip10 +,pip18 +.
注意:我是该工具的作者.
小智 72
您只需打印过时的软件包即可
pip freeze | cut -d = -f 1 | xargs -n 1 pip search | grep -B2 'LATEST:'
Run Code Online (Sandbox Code Playgroud)
rar*_*iru 60
以下单行可能会有所帮助:
pip list --format freeze --outdated | sed 's/(.*//g' | xargs -n1 pip install -U
xargs -n1
如果发生错误,继续前进.
如果您需要对省略的内容以及引发错误的内容进行更细致的"细粒度"控制,则不应添加-n1
标志并明确定义要忽略的错误,通过为每个单独的错误"管道"以下行:
| sed 's/^<First characters of the error>.*//'
这是一个工作示例:
pip list --format freeze --outdated | sed 's/(.*//g' | sed 's/^<First characters of the first error>.*//' | sed 's/^<First characters of the second error>.*//' | xargs pip install -U
Run Code Online (Sandbox Code Playgroud)
Mar*_*arc 57
这个选项在我看来更直接和可读:
pip install -U `pip list --outdated | tail -n +3 | awk '{print $1}'`
Run Code Online (Sandbox Code Playgroud)
解释是以pip list --outdated
这种格式输出所有过时包的列表:
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
Run Code Online (Sandbox Code Playgroud)
tail -n +3
跳过前两行并awk '{print $1}'
选择每行的第一个单词.
Dou*_*eco 42
更强大的解决方案
对于pip3使用此:
pip3 freeze --local |sed -rn 's/^([^=# \t\\][^ \t=]*)=.*/echo; echo Processing \1 ...; pip3 install -U \1/p' |sh
Run Code Online (Sandbox Code Playgroud)
对于pip,只需删除3s:
pip freeze --local |sed -rn 's/^([^=# \t\\][^ \t=]*)=.*/echo; echo Processing \1 ...; pip install -U \1/p' |sh
Run Code Online (Sandbox Code Playgroud)
OSX奇怪
截至2017年7月,OSX发布了一个非常旧版本的sed(十几岁).要获得扩展的正则表达式,请在上面的解决方案中使用-E而不是-r.
解决流行解决方案的问题
该解决方案经过精心设计和测试1,而即使是最流行的解决方案也存在问题.
上面的命令使用最简单和最便携的pip语法结合sed和sh来完全克服这些问题.可以使用注释版本2仔细检查sed操作的详细信息.
细节
[1]在Linux 4.8.16-200.fc24.x86_64集群中经过测试和定期使用,并在其他五种Linux/Unix版本上进行了测试.它还运行在Windows 10上安装的Cygwin64上.需要在iOS上进行测试.
[2]为了更清楚地看到命令的解剖结构,这与带有注释的上述pip3命令完全相同:
# match lines from pip's local package list output
# that meet the following three criteria and pass the
# package name to the replacement string in group 1.
# (a) Do not start with invalid characters
# (b) Follow the rule of no white space in the package names
# (c) Immediately follow the package name with an equal sign
sed="s/^([^=# \t\\][^ \t=]*)=.*"
# separate the output of package upgrades with a blank line
sed="$sed/echo"
# indicate what package is being processed
sed="$sed; echo Processing \1 ..."
# perform the upgrade using just the valid package name
sed="$sed; pip3 install -U \1"
# output the commands
sed="$sed/p"
# stream edit the list as above
# and pass the commands to a shell
pip3 freeze --local |sed -rn "$sed" |sh
Run Code Online (Sandbox Code Playgroud)
[3]升级也用于升级Python或PIP组件的Python或PIP组件可能是导致死锁或程序包数据库损坏的潜在原因.
Shi*_* Xu 37
这似乎更简洁.
pip list --outdated | cut -d ' ' -f1 | xargs -n1 pip install -U
Run Code Online (Sandbox Code Playgroud)
说明:
pip list --outdated
获得这样的线条
urllib3 (1.7.1) - Latest: 1.15.1 [wheel]
wheel (0.24.0) - Latest: 0.29.0 [wheel]
Run Code Online (Sandbox Code Playgroud)
在cut -d ' ' -f1
,-d ' '
将"space"设置为分隔符,-f1
表示获取第一列.
所以上面的行变为:
urllib3
wheel
Run Code Online (Sandbox Code Playgroud)
然后将它们传递xargs
给运行命令,pip install -U
每行作为附加参数
-n1
将传递给每个命令的参数数量限制pip install -U
为1
Sim*_*aei 31
升级时遇到了同样的问题.事实是,我永远不会升级所有包裹.我只升级我需要的东西,因为项目可能会中断.
因为没有简单的方法来逐个包升级,并且更新了requirements.txt文件,所以我编写了这个pip-upgrader,它还会更新requirements.txt
文件中所选包(或所有包)的版本.
安装
pip install pip-upgrader
Run Code Online (Sandbox Code Playgroud)
用法
激活你的virtualenv(重要的是,因为它还将在当前的virtualenv中安装新版本的升级包).
cd
进入你的项目目录,然后运行:
pip-upgrade
Run Code Online (Sandbox Code Playgroud)
高级用法
如果要求放在非标准位置,请将它们作为参数发送:
pip-upgrade path/to/requirements.txt
Run Code Online (Sandbox Code Playgroud)
如果您已经知道要升级的包,只需将它们作为参数发送:
pip-upgrade -p django -p celery -p dateutil
Run Code Online (Sandbox Code Playgroud)
如果需要升级到预发布/发布后版本,--prerelease
请在命令中添加参数.
完全披露:我写了这个包.
小智 26
来自https://github.com/cakebread/yolk:
$ pip install -U `yolk -U | awk '{print $1}' | uniq`
Run Code Online (Sandbox Code Playgroud)
但是你需要先得到蛋黄:
$ sudo pip install -U yolk
Run Code Online (Sandbox Code Playgroud)
Sal*_*bas 25
@ Ramana答案的单行版本.
python -c 'import pip, subprocess; [subprocess.call("pip install -U " + d.project_name, shell=1) for d in pip.get_installed_distributions()]'
Run Code Online (Sandbox Code Playgroud)
`
小智 17
当使用virtualenv并且如果您只想升级添加到virtualenv的软件包时,您可能希望:
pip install `pip freeze -l | cut --fields=1 -d = -` --upgrade
Run Code Online (Sandbox Code Playgroud)
Ape*_*ime 16
Windows Powershell解决方案
pip freeze | %{$_.split('==')[0]} | %{pip install --upgrade $_}
Run Code Online (Sandbox Code Playgroud)
Red*_*yed 14
我在pip问题讨论中找到的最简单,最快速的解决方案是:
sudo -H pip install pipdate
sudo -H pipdate
Run Code Online (Sandbox Code Playgroud)
来源:https://github.com/pypa/pip/issues/3819
小智 12
你可以试试这个:
for i in ` pip list|awk -F ' ' '{print $1}'`;do pip install --upgrade $i;done
Run Code Online (Sandbox Code Playgroud)
use*_*849 12
相当神奇的蛋黄使这很容易.
pip install yolk3k # don't install `yolk`, see https://github.com/cakebread/yolk/issues/35
yolk --upgrade
Run Code Online (Sandbox Code Playgroud)
有关yolk的更多信息:https://pypi.python.org/pypi/yolk/0.4.3
它可以做很多你可能会发现有用的东西.
Joh*_*DHH 12
使用awk更新包:
pip install -U $(pip freeze | awk -F'[=]' '{print $1}')
windows powershell更新
foreach($p in $(pip freeze)){ pip install -U $p.Split("=")[0]}
Ger*_*ich 12
实现这一目标的纯Bash / Z shell one-liner:
for p in $(pip list -o --format freeze); do pip install -U ${p%%=*}; done
Run Code Online (Sandbox Code Playgroud)
或者,以一种格式良好的方式:
for p in $(pip list -o --format freeze)
do
pip install -U ${p%%=*}
done
Run Code Online (Sandbox Code Playgroud)
小智 12
在 Windows 或 Linux 上更新 Python 包
1-将已安装的软件包列表输出到需求文件 (requirements.txt) 中:
pip freeze > requirements.txt
Run Code Online (Sandbox Code Playgroud)
2- 编辑requirements.txt,并将所有'=='替换为'>='。在编辑器中使用“全部替换”命令。
3 - 升级所有过时的软件包
pip install -r requirements.txt --upgrade
Run Code Online (Sandbox Code Playgroud)
来源:https : //www.activestate.com/resources/quick-reads/how-to-update-all-python-packages/
Lin*_*SEO 11
没必要那么麻烦或者安装一些包。
在Linux shell上更新 pip 包:
pip list --outdated --format=freeze | awk -F"==" '{print $1}' | xargs -i pip install -U {}
Run Code Online (Sandbox Code Playgroud)
在Windows powershell上更新 pip 包:
pip list --outdated --format=freeze | ForEach { pip install -U $_.split("==")[0] }
Run Code Online (Sandbox Code Playgroud)
几点:
pip
为pip3
或pip2
。pip list --outdated
检查过时的 pip 包。--format
在我的 pip 版本 22.0.3 上只有 3 种类型:columns (默认)、 freeze 或 json。freeze
是命令管道中更好的选择。chb*_*own 10
@ Ramana的回答对我来说是最好的,在这里,但我不得不添加一些捕获:
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
Run Code Online (Sandbox Code Playgroud)
该site-packages
检查不包括我的开发包,因为它们不在系统site-packages目录中.try-except只是跳过已从PyPI中删除的包.
@endolith:我也希望有一个简单的pip.install(dist.key, upgrade=True)
,但它看起来并不像pip被命令行所使用的任何东西(文档没有提到内部API,而且pip开发人员没有使用文档字符串).
通过拉动请求发送到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
Run Code Online (Sandbox Code Playgroud)
这似乎对我有用......
pip install -U $(pip list --outdated|awk '{printf $1" "}')
Run Code Online (Sandbox Code Playgroud)
之后我用printf
空格来正确分隔包名.
在pip_upgrade_outdated
做这项工作.根据其文档:
usage: pip_upgrade_outdated [-h] [-3 | -2 | --pip_cmd PIP_CMD]
[--serial | --parallel] [--dry_run] [--verbose]
[--version]
Upgrade outdated python packages with pip.
optional arguments:
-h, --help show this help message and exit
-3 use pip3
-2 use pip2
--pip_cmd PIP_CMD use PIP_CMD (default pip)
--serial, -s upgrade in serial (default)
--parallel, -p upgrade in parallel
--dry_run, -n get list, but don't upgrade
--verbose, -v may be specified multiple times
--version show program's version number and exit
Run Code Online (Sandbox Code Playgroud)
步骤1:
pip install pip-upgrade-outdated
Run Code Online (Sandbox Code Playgroud)
第2步:
pip_upgrade_outdated
Run Code Online (Sandbox Code Playgroud)
Windows上最短,最简单的.
pip freeze > requirements.txt && pip install --upgrade -r requirements.txt && rm requirements.txt
Run Code Online (Sandbox Code Playgroud)
这不是更有效吗?
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
Run Code Online (Sandbox Code Playgroud)
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
从它左边的管道中取出一个参数,并将其传递给命令以升级包列表.我的剧本:
pip list --outdated --format=legacy | cut -d ' ' -f1 | xargs -n1 pip install --upgrade
Run Code Online (Sandbox Code Playgroud)
这是针对Python 3的PowerShell解决方案:
pip3 list --outdated --format=legacy | ForEach { pip3 install -U $_.split(" ")[0] }
Run Code Online (Sandbox Code Playgroud)
对于Python 2:
pip2 list --outdated --format=legacy | ForEach { pip2 install -U $_.split(" ")[0] }
Run Code Online (Sandbox Code Playgroud)
这将一个接一个地升级软件包。所以
pip3 check
pip2 check
Run Code Online (Sandbox Code Playgroud)
之后应确保没有依赖项被破坏。
这是virtualenv
通过 pip更新所有 Python 3 包(在激活的)中的代码:
import pkg_resources
from subprocess import call
for dist in pkg_resources.working_set:
call("python3 -m pip install --upgrade " + dist.project_name, shell=True)
Run Code Online (Sandbox Code Playgroud)
这是我对rbp's answer 的变体,它绕过了“可编辑”和开发发行版。它具有原始版本的两个缺陷:不必要地重新下载和重新安装;并且一个包上的错误将阻止此后的每个包的升级。
pip freeze |sed -ne 's/==.*//p' |xargs pip install -U --
Run Code Online (Sandbox Code Playgroud)
相关的错误报告,从 Bitbucket 迁移后有点脱节:
小智 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)
Run Code Online (Sandbox Code Playgroud)
对于不输出为旧格式的新版本 pip(版本 18+):
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)
Run Code Online (Sandbox Code Playgroud)
import pip
pkgs = [p.key for p in pip.get_installed_distributions()]
for pkg in pkgs:
pip.main(['install', '--upgrade', pkg])
Run Code Online (Sandbox Code Playgroud)
甚至:
import pip
commands = ['install', '--upgrade']
pkgs = commands.extend([p.key for p in pip.get_installed_distributions()])
pip.main(commands)
Run Code Online (Sandbox Code Playgroud)
快速工作,因为它不会不断发布shell.我很想找到时间来实际使用过时的列表来加快速度.
我最近一直在用pur。这很简单,也很切题。它会更新您的requirements.txt
文件以反映升级,然后您可以requirements.txt
照常使用您的文件进行升级。
$ pip install pur
...
Successfully installed pur-4.0.1
$ pur
Updated boto3: 1.4.2 -> 1.4.4
Updated Django: 1.10.4 -> 1.10.5
Updated django-bootstrap3: 7.1.0 -> 8.1.0
All requirements up-to-date.
$ pip install --upgrade -r requirements.txt
Successfully installed Django-1.10.5 ...
Run Code Online (Sandbox Code Playgroud)
PowerShell 5.1中有一行使用adm权限,python 3.6.5和pip ver 10.0.1:
pip list -o --format json | ConvertFrom-Json | foreach {pip install $_.name -U --no-warn-script-location}
Run Code Online (Sandbox Code Playgroud)
如果列表中没有破损的包裹或特殊轮子,它可以顺利运行......
查看所有过时的软件包
pip list --outdated --format=columns
Run Code Online (Sandbox Code Playgroud)
安装
sudo pip install pipdate
Run Code Online (Sandbox Code Playgroud)
然后输入
sudo -H pipdate
Run Code Online (Sandbox Code Playgroud)
一个 JSON + jq答案:
pip list -o --format json | jq '.[] | .name' | xargs pip install -U
Run Code Online (Sandbox Code Playgroud)
下面的 Windowscmd
代码段执行以下操作:
- 升级
pip
到最新版本。- 升级所有过时的软件包。
- 对于正在升级的每个包,检查
requirements.txt
任何版本说明符。
@echo off
Setlocal EnableDelayedExpansion
rem /sf/ask/190401011/
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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1076379 次 |
最近记录: |