多线程自动检测并安装python数据库

多线程自动检测并安装python数据库


概述

  Python的魅力在于有很多免费开源的三方数据库。我们在创作python项目时,经常会调用一些三方数据库来进行使用。比如数据分析经常会用到pandas、numpy、matplotlib、plotly、pyecharts等等三方数据库,网络爬虫会经常用到requests、lxml、BeautifulSoup、selenium、scrapy等等三方数据库。


  我们一般安装需要使用pip命令来进行手动的安装。本案例通过python实现了多线程自动检测并安装自己所需的数据库。

提示:以下是本篇文章正文内容,基于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!")

总结

  以上就是今天要展示的内容,本案例使用了线程池的方法,多线程的自动检测并安装数据库。使用了面向对象来编写,用户可以根据自己的需求,在类中按照模板自由的增加安装其他数据库的方法。


  如果您有更好的意见,欢迎在评论区发表!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值