多线程自动检测并安装python数据库
概述
Python的魅力在于有很多免费开源的三方数据库。我们在创作python项目时,经常会调用一些三方数据库来进行使用。比如数据分析经常会用到pandas、numpy、matplotlib、plotly、pyecharts等等三方数据库,网络爬虫会经常用到requests、lxml、BeautifulSoup、selenium、scrapy等等三方数据库。提示:以下是本篇文章正文内容,基于python3环境运行。
一、源码实例如下所示
# -*- coding: utf-8 -*-
"""
# @Language: Python3
"""
import os
from concurrent.futures import ThreadPoolExecutor
class DatabaseInstallation():
""" 一个自动检测安装数据库的类 """
def __init__(self):
""" 初始化默认属性 """
def install_pandas(self):
""" 自动检测安装 pandas """
try:
import pandas as pd
except:
print("\npandas is not installed. Start installation, please wait...")
os.system(command="pip install pandas -i https://pypi.doubanio.com/simple/")
else:
print("\n\tpandas has been installed.")
return
def install_numpy(self):
""" 自动检测安装 numpy """
try:
import numpy as np
except:
print("\nnumpy is not installed. Start installation, please wait...")
os.system(command="pip install numpy -i https://pypi.doubanio.com/simple/")
else:
print("\n\tnumpy has been installed.")
return
def install_xlwings(self):
""" 自动检测安装 xlwings """
try:
import xlwings as xlg
except:
print("\nxlwings is not installed. Start installation, please wait...")
os.system(command="pip install xlwings -i https://pypi.doubanio.com/simple/")
else:
print("\n\txlwings has been installed.")
return
def install_matplotlib(self):
""" 自动检测安装 matplotlib """
try:
from matplotlib import pyplot as plt
except:
print("\nmatplotlib is not installed. Start installation, please wait...")
os.system(command="pip install matplotlib -i https://pypi.doubanio.com/simple/")
else:
print("\n\tmatplotlib has been installed.")
return
def install_openpyxl(self):
""" 自动检测安装 openpyxl """
try:
import openpyxl as opxl
except:
print("\nopenpyxl is not installed. Start installation, please wait...")
os.system(command="pip install openpyxl -i https://pypi.doubanio.com/simple/")
else:
print("\n\topenpyxl has been installed.")
return
def install_plotly(self):
""" 自动检测安装 plotly==4.14.3. 因为5.0版本移除了对比交互式按钮, 所以还是4.14.3最好用 """
try:
import plotly
from plotly import graph_objects as go
except:
print("\nplotly is not installed. Start installation, please wait...")
os.system(command="pip install plotly==4.14.3 -i https://pypi.doubanio.com/simple/")
else:
print("\n\tplotly has been installed.")
return
def install_xlsxwriter(self):
""" 自动检测安装 xlsxwriter """
try:
import xlsxwriter as xlr
except:
print("\nxlsxwriter is not installed. Start installation, please wait...")
os.system(command="pip install xlsxwriter -i https://pypi.doubanio.com/simple/")
else:
print("\n\txlsxwriter has been installed.")
return
def install_requests(self):
""" 自动检测安装 requests """
try:
import requests
except:
print("\nrequests is not installed. Start installation, please wait...")
os.system(command="pip install requests -i https://pypi.doubanio.com/simple/")
else:
print("\n\trequests has been installed.")
return
def install_lxml(self):
""" 自动检测安装 lxml """
try:
from lxml import etree
except:
print("\nlxml is not installed. Start installation, please wait...")
os.system(command="pip install lxml -i https://pypi.doubanio.com/simple/")
else:
print("\n\tlxml has been installed.")
return
def install_pyfiglet(self):
""" 自动检测安装 pyfiglet 字符画模块 """
try:
from pyfiglet import Figlet, FigletFont
except:
print("\npyfiglet is not installed. Start installation, please wait...")
os.system(command="pip install pyfiglet -i https://pypi.doubanio.com/simple/")
else:
print("\n\tpyfiglet has been installed.")
return
def install_pygal(self):
""" 自动检测安装 pygal """
try:
import pygal
except:
print("\npygal is not installed. Start installation, please wait...")
os.system(command="pip install pygal -i https://pypi.doubanio.com/simple/")
else:
print("\n\tpygal has been installed.")
return
def install_pyecharts(self):
""" 自动检测安装 pyecharts """
try:
from pyecharts.charts import Bar, Line, Scatter
except:
print("\npyecharts is not installed. Start installation, please wait...")
os.system(command="pip install pyecharts -i https://pypi.doubanio.com/simple/")
else:
print("\n\tpyecharts has been installed.")
return
def install_selenium(self):
""" 自动检测安装 selenium """
try:
from selenium.webdriver import Chrome, Edge, Firefox
except:
print("\nselenium is not installed. Start installation, please wait...")
os.system(command="pip install selenium -i https://pypi.doubanio.com/simple/")
else:
print("\n\tselenium has been installed.")
return
if __name__ == '__main__':
db = DatabaseInstallation() # 实例化一个数据库对象
with ThreadPoolExecutor(20) as t_pool: # 创建一个线程池,设置最大支持20个线程同时进行
""" 多线程安装数据库 """
t_pool.submit(db.install_openpyxl) # 自动检测安装 openpyxl
t_pool.submit(db.install_xlsxwriter) # 自动检测安装 xlsxwriter
t_pool.submit(db.install_pandas) # 自动检测安装 pandas
t_pool.submit(db.install_numpy) # 自动检测安装 numpy
t_pool.submit(db.install_matplotlib) # 自动检测安装 matplotlib
t_pool.submit(db.install_plotly) # 自动检测安装 plotly==4.14.3
t_pool.submit(db.install_pyfiglet) # 自动检测安装字符画 pyfiglet
t_pool.submit(db.install_pyecharts) # 自动检测安装 pyecharts
t_pool.submit(db.install_requests) # 自动检测安装 requests
t_pool.submit(db.install_lxml) # 自动检测安装 lxml
""" 展示软件版本信息 """
import openpyxl
import xlsxwriter
import pandas
import numpy
import matplotlib
import plotly
import pyfiglet
import pyecharts
import requests
import lxml
print("\n")
print("******************************************")
print("* Your database version is as follows:")
print("* openpyxl:", openpyxl.__version__)
print("* xlsxwriter:", xlsxwriter.__version__)
print("* pandas:", pandas.__version__)
print("* numpy:", numpy.__version__)
print("* matplotlib:", matplotlib.__version__)
print("* plotly:", plotly.__version__)
print("* pyfiglet:", pyfiglet.__version__)
print("* pyecharts:", pyecharts.__version__)
print("* requests:", requests.__version__)
print("* lxml:", lxml.__version__)
print("******************************************")
input("\nClick enter to exit!")