Python3 全自动更新已安装的模块实现

yipeiwu_com7年前Python基础

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设计】。

相关文章

django序列化serializers过程解析

这篇文章主要介绍了django序列化serializers过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 DRF的核心 就是...

TensorFlow实现随机训练和批量训练的方法

TensorFlow实现随机训练和批量训练的方法

TensorFlow更新模型变量。它能一次操作一个数据点,也可以一次操作大量数据。一个训练例子上的操作可能导致比较“古怪”的学习过程,但使用大批量的训练会造成计算成本昂贵。到底选用哪种训...

Python实现的拉格朗日插值法示例

本文实例讲述了Python实现的拉格朗日插值法。分享给大家供大家参考,具体如下: 拉格朗日插值简单介绍 拉格朗日插值法是以法国十八世纪数学家约瑟夫·拉格朗日命名的一种多项式插值方法。 许...

python 接口实现 供第三方调用的例子

python 接口实现 供第三方调用的例子

实验环境 1.环境问题 python 2.7 以上自带的pyunit bottle 作为一个python的简易服务器 在python安装目录 打开命令窗口(具体 shift+鼠标右键)...

Python实现基本数据结构中队列的操作方法示例

Python实现基本数据结构中队列的操作方法示例

本文实例讲述了Python实现基本数据结构中队列的操作方法。分享给大家供大家参考,具体如下: #! /usr/bin/env python #coding=utf-8 class Q...