Python3 全自动更新已安装的模块实现
1. 手动操作
1.1. 显示模块
pip list
1.2. 显示过期模块
pip list --outdated
1.3. 安装模块
pip install xxx
1.4. 升级模块
pip install --upgrade xxx
2. 自动操作
手动敲命令升级有点儿麻烦(特别是需要更新的模块比较多时),而我们完全可以用代码简单地实现全自动升级。
代码可以至GitHub下载,也可以复制本文中的代码:

autoUpgradePythonModules.py:
import subprocess
import os
command = "pip list --outdated"
print('正在获取需要升级的模块信息,请稍后...')
print('Getting the information of outdated modules, wait a moment...')
print()
outdatelist = subprocess.Popen (command, stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell = True).stdout.readlines()
updatelist = []
#print(outdatelist)
for i in outdatelist:
i = str(i, encoding='utf-8')
print(i,end='')
i = i[:i.find(' ')]
updatelist.append(i)
#print('\n', i, len(i))
updatelist = updatelist[2:]
#print(updatelist)
c = 1
total = len(updatelist)
if updatelist :
for x in updatelist:
print('\n', c, '/', total, ' upgrading ', x, sep='')
c += 1
tempcmd = "pip install --upgrade " + x
os.system(tempcmd)
print("所有模块都已更新完毕!!")
print('All modules have been updated.')
else :
print("没有模块需要更新!!")
print('All modules is updated.')
print('请按回车键以退出程序。')
print('Press enter key to quit.')
input()
Windows平台下可以运行下面的脚本,该脚本会自动获取管理员权限并进行更新(安装在C盘或者其他一些特殊的目录下可能需要管理员权限才能更新)。
autoUpgradePythonModules.bat:
@echo off
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
start python autoUpgradePythonModules.py
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
python使用BeautifulSoup分析网页信息的方法
这篇文章主要介绍了python使用BeautifulSoup分析网页信息的方法,涉及Python使用BeautifulSoup模块分析网页信息的技巧,非常具有实用价值,需要的朋友可以参考下2015-04-04
Pytorch中Softmax与LogSigmoid的对比分析
这篇文章主要介绍了Pytorch中Softmax与LogSigmoid的对比分析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-06-06
Python3+Pycharm+PyQt5环境搭建步骤图文详解
这篇文章主要介绍了Python3+Pycharm+PyQt5环境搭建步骤图文详解,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下2019-05-05
如何将Pycharm中Terminal使用Powershell作为终端
这篇文章主要介绍了如何将Pycharm中Terminal使用Powershell作为终端问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-05-05
python 中的collections.OrderedDict() 用法
这篇文章主要介绍了python 中的collections.OrderedDict() 用法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-05-05


最新评论