如下:
# _*_coding:utf-8_*_
import os
import json
from subprocess import call
# pip源
index_url = 'https://mirrors.aliyun.com/pypi/simple'
trusted_host = 'mirrors.aliyun.com'
print('Checking for updatable packages...\n')
data = os.popen('pip list --outdated --format=json')
data = data.read()
data = json.loads(data)
if data:
print(str(len(data)) + ' packages can be upgraded.')
for dist in data:
print('\n' + dist['name'])
cmd = 'pip3 install --timeout 6000 --trusted-host ' + trusted_host + \
' --index-url ' + index_url + ' --upgrade ' + dist['name']
call(cmd, shell=True)
print('\nUpdate completed!')
else:
print('\nAll packages are the latest version.')
input('\nPress any key to exit.')