栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

多线程自动检测并安装python数据库
  • 概述
  • 一、源码实例如下所示
  • 总结


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


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

提示:以下是本篇文章正文内容,基于python3环境运行。

一、源码实例如下所示
# -*- coding: utf-8 -*-
"""
# @Language: Python3
# @Author : Shusheng Yuan
# @Email : shusheng.yuan@foxmail.com
# @File : Automatically_detect_and_install_the_databases.py
# @Time : 2021/10/2 13:12
"""

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("ntpandas 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("ntnumpy 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("ntxlwings 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("ntmatplotlib 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("ntopenpyxl 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("ntplotly 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("ntxlsxwriter 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("ntrequests 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("ntlxml 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("ntpyfiglet 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("ntpygal 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("ntpyecharts 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("ntselenium 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!")
总结   以上就是今天要展示的内容,本案例使用了线程池的方法,多线程的自动检测并安装数据库。使用了面向对象来编写,用户可以根据自己的需求,在类中按照模板自由的增加安装其他数据库的方法。


  如果您有更好的意见,欢迎在评论区发表!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/286317.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号