是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
是否可以使用pip一次性升级所有Python包?
注意:官方问题跟踪器上对此有一个功能请求。
当前回答
我在升级方面也遇到了同样的问题。问题是,我从不升级所有包。我只升级我需要的,因为项目可能会中断。
因为没有一种简单的方法来逐个包升级和更新requirements.txt文件,所以我编写了这个pip升级程序,它还更新了所选包(或所有包)的requirements.txt文件中的版本。
安装
pip install pip-upgrader
用法
激活virtualenv(这很重要,因为它还将在当前virtualenv中安装升级包的新版本)。
cd到项目目录中,然后运行:
pip-upgrade
高级用法
如果需求放置在非标准位置,请将其作为参数发送:
pip-upgrade path/to/requirements.txt
如果您已经知道要升级的软件包,只需将其作为参数发送:
pip-upgrade -p django -p celery -p dateutil
如果需要升级到发布前/发布后版本,请在命令中添加--prerelease参数。
完全披露:我写了这个包裹。
其他回答
拉玛纳的回答对我来说是最好的,但我不得不补充几点:
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
站点包检查排除了我的开发包,因为它们不在系统站点包目录中。try-except只是跳过已从PyPI中删除的包。
对于endolith:我也希望有一个简单的pip.install(dist.key,upgrade=True),但它看起来不像是要让命令行以外的任何东西使用pip(文档没有提到内部API,pip开发人员也没有使用docstring)。
如果您希望升级仅由pip安装,并且避免升级由其他工具(如apt、yum等)安装的软件包,那么您可以使用我在Ubuntu上使用的脚本(可能也适用于其他发行版)-基于以下帖子:
printf "To update with pip: pip install -U"
pip list --outdated 2>/dev/null | gawk '{print $1;}' | while read; do pip show "${REPLY}" 2>/dev/null | grep 'Location: /usr/local/lib/python2.7/dist-packages' >/dev/null; if (( $? == 0 )); then printf " ${REPLY}"; fi; done; echo
如果您安装了pip<22.3,则使用纯Bash/Z外壳一个衬垫实现:
for p in $(pip list -o --format freeze); do pip install -U ${p%%=*}; done
或者,以一种格式良好的方式:
for p in $(pip list -o --format freeze)
do
pip install -U ${p%%=*}
done
在这之后,您将得到pip>=22.3,其中-o和--format冻结是互斥的,您不能再使用这一行。
通过拉取请求发送给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
Use:
pip install -r <(pip freeze) --upgrade