python导入管理(环境初始化)

  • Post author:
  • Post category:python


# 下载源
pipy_url = '-i http://pypi.douban.com/simple --trusted-host pypi.douban.com'
# 下载包的库版本
import_list = {'selenium': 'selenium==3.141.0', 'requests': 'requests==2.28.1', 'dateutil': 'python-dateutil==2.8.2',
               'PIL': 'Pillow==9.4.0', 'jsonpath': 'jsonpath==0.82', 'psutil': 'psutil==5.9.4',
               'cssselect': 'cssselect==1.2.0', 'pypinyin': 'pypinyin==0.48.0', 'cv2': 'opencv-python==4.5.5.64',
               'jieba': 'jieba==0.42.1', 'tinydb': 'tinydb==4.7.1','fastapi':'fastapi==0.95.1','loguru':'loguru==0.7.0',
               'webdriver_manager':'webdriver_manager==3.8.5','pymysql':'PyMySQL==1.0.3','pandas':'pandas==2.0.1',
               'redis':'redis==4.5.5'}
 # 循环引用库,比库列表中多一个
for item in range(len(import_list)+1):
    try:
        import jieba
        import psutil
        import requests
        import pypinyin
        from cv2 import cv2
        from PIL import Image
        from loguru import logger
        from fastapi import FastAPI
        from jsonpath import jsonpath
        from selenium import webdriver
        from tinydb import TinyDB, Query
        from cssselect.xpath import GenericTranslator
        from dateutil.relativedelta import relativedelta
        from webdriver_manager.chrome import ChromeDriverManager
        import pandas as pd
        break
    except ImportError as e:
    	# 获取不存在的库,并在库字典中找对应的版本
        module = e.msg.replace('No module named ','').replace("'",'')
        os.system(f'pip install {import_list.get(module)} {pipy_url}')



版权声明:本文为qq_45878803原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。