关于自己写的一个检测本地是否有安装第三方库的脚本问题,使用while嵌套try-except安装后,导入的包不能被正确识别

如题,关于自己写的一个检测本地是否有安装第三方库的脚本问题,使用while嵌套try-except安装后,导入的包不能被正确识别。但明明是已经正确安装了,第二次运行脚本就可以使用了。因为目的是再内网写的一些个小工具小脚本,会在别人的没有链接外网的电脑上运行,所以需要有一个测本地是否有安装第三方库的脚本,最好是一键能搞定,我现在的环境是py311

import os
import Importwhl

count =3 
while count:
    try:
        import xlwings as xw
        print("xlwings is installed")
        break
    except ModuleNotFoundError:
        print("Load Module failed")
        Importwhl.importwhl()
        continue

if __name__ == "__main__":
    print('abc')
    xw.App()

importwhl是自己写的脚本,用于根据py版本匹配本地的第三方库


import os
import sys

libs=['pywin32','xlwings']
pyvers=(('3.11','cp311'),('3.10','cp310'),('3.9','cp39'),('3.8','cp38')\
        ,('3.7','cp37'))#最少要3.6才能使用xlwings,但官方包只提供到3.7
pftags=(('win32','win32'),('amd64','win_amd64'))


def importwhl():
    try:
        import xlwings
    except ModuleNotFoundError:
        print("Load Module failed")
        version = sys.version.lower()
        tag='win_amd64'
        pyv='cp37'

        for t in pftags:
            if t[0] in version:
                tag=t[1]
                break
        for v in pyvers:
            if v[0] in version:
                pyv = v[1]
        libpath=os.getcwd()+'\\'+'libs'+'\\'
        moudle=''
        for lib in libs:
            for moudle in os.listdir (libpath):
                if lib in moudle and tag in moudle \
                and pyv in moudle:
                    print('install'+os.getcwd()+'\\'+'libs'+'\\'+moudle)
                    os.system('pip install '+os.getcwd()+'\\'+'libs'+'\\'+moudle)

运行结果如下

Load Module failed
Load Module failed
installC:\Code\python\Exceltest\libs\pywin32-305-cp311-cp311-win_amd64.whl
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Processing c:\code\python\exceltest\libs\pywin32-305-cp311-cp311-win_amd64.whl
Installing collected packages: pywin32
Successfully installed pywin32-305

[notice] A new release of pip available: 22.3 -> 22.3.1
[notice] To update, run: python.exe -m pip install --upgrade pip
installC:\Code\python\Exceltest\libs\xlwings-0.28.8-cp311-cp311-win_amd64.whl
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Processing c:\code\python\exceltest\libs\xlwings-0.28.8-cp311-cp311-win_amd64.whl
Requirement already satisfied: pywin32>=224 in c:\users\soki\appdata\local\programs\python\python311\lib\site-packages (from xlwings==0.28.8) (305)
Installing collected packages: xlwings
Successfully installed xlwings-0.28.8

[notice] A new release of pip available: 22.3 -> 22.3.1
[notice] To update, run: python.exe -m pip install --upgrade pip
xlwings is installed
abc

关于自己写的一个检测本地是否有安装第三方库的脚本问题,使用while嵌套try-except安装后,导入的包不能被正确识别
我停掉再运行就能正确运行这个了,但是疑惑为什么这个xw没有被识别,

讨论数量: 3
Jason990420
xw.App()

AttributeError: 'NoneType' object has no attribute 'apps'

not

AttributeError: 'NoneType' object has no attribute 'App' ?

2周前 评论
Eolopola (楼主) 2周前
Eolopola (楼主) 2周前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!