python运行使用pywin32下载及安装问题

发布时间 2023-05-24 10:15:21作者: zhwy524

 1、python执行报错 File "F:\XXX\XXX.py", line 7, in <module>

 

运行python程序,提示错误如下:

import win32gui ModuleNotFoundError: No module named 'win32gui'

该报错信息表示在F:\XXX\XXX.py中的第7行引入了win32gui,但是当前设备缺少pywin32的模块,需要安装

安装方式一:直接在线安装

pip install pywin32

安装方式二:离线安装

如果pip无法安装pywin32,可以在pywin32的官网(https://github.com/mhammond/pywin32/releases)上下载对应操作系统版本的安装包

本机环境为64位操作系统,python3.9,所以选择的安装文件为pywin32-306.win-amd64-py3.9.exe

 

安装即可

 2、安装pywin32出现--Python version 3.9 required, which was not found in the registry

安装pywin32出现--Python version 3.9 required, which was not found in the registry

或者

pywin32出现--Python version 3.9-32 required, which was not found in the registry

 

解决方法:

步骤一:打开注册表

在运行中输入regedit

步骤二:查找注册表的python

在HKEY_CURRENT_USER\Software中找一下是否存在python注册信息

 

如果没有python的文件夹,则执行下面命令

from __future__ import print_function
 
import sys
 
try:
    from winreg import *
except ImportError:
    from _winreg import *
 
# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix
 
regpath = "SOFTWARE\\Python\\Pythoncore\\{0}\\".format(version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "{0};{1}\\Lib\\;{2}\\DLLs\\".format(
    installpath, installpath, installpath)
 
 
def RegisterPy():
    try:
        reg = OpenKey(HKEY_CURRENT_USER, regpath)
    except EnvironmentError as e:
        try:
            reg = CreateKey(HKEY_CURRENT_USER, regpath)
            SetValue(reg, installkey, REG_SZ, installpath)
            SetValue(reg, pythonkey, REG_SZ, pythonpath)
            CloseKey(reg)
        except:
            print("*** Unable to register!")
            return
        print("--- Python", version, "is now registered!")
        return
    if (QueryValue(reg, installkey) == installpath and
            QueryValue(reg, pythonkey) == pythonpath):
        CloseKey(reg)
        print("=== Python", version, "is already registered!")
        return
    CloseKey(reg)
    print("*** Unable to register!")
    print("*** You probably have another Python installation!")
 
 
if __name__ == "__main__":
    RegisterPy()

 在python console执行后,会显示--- Python 3.9 is now registered!,即表示注册成功

 

 

步骤三:再次进入注册表,查看已经存在python的文件夹

 

如果是提示Python version 3.9-32 required, which was not found in the registry的,修改为3.9-32即可

步骤四:重新执行pywin32-306.win-amd64-py3.9.exe,安装成功