升级了win11之后,我发现pywin32无法使用了。明明已经安装了pywin32,但每次import win32gui都会提示这个报错:
ImportError: DLL load failed while importing win32gui: 找不到指定的程序。
搜了一下也没有网友提供解决方法,于是上官网去找终于找到了,亲测有效:
-
在 Python 中安装 pywin32 库
pip install pywin32
-
安装完成后找到自己的 Python 根目录,在该目录下打开命令行。
比如我使用的时 conda 的环境,目录是 D:ProgramDataAnaconda3envstorch,我就在命令行中 cd 到这个目录,然后 conda activate torch。如果是普通的 Python 就不用 conda activate 这步了。
在命令行中输入:
python.exe Scripts/pywin32_postinstall.py -install
执行后显示以下信息,问题即可解决。
Parsed arguments are: Namespace(install=True, remove=False, wait=None, silent=False, quiet=False, destination='D:\ProgramData\Anaconda3\envs\torch\Lib\site-packages') Copied pythoncom39.dll to D:ProgramDataAnaconda3envstorchpythoncom39.dll Copied pywintypes39.dll to D:ProgramDataAnaconda3envstorchpywintypes39.dll You do not have the permissions to install COM objects. The sample COM objects were not registered. NOTE: PyWin32.chm can not be located, so has not been registered Registered help file Pythonwin has been registered in context menu Creating directory D:ProgramDataAnaconda3envstorchLibsite-packageswin32comgen_py Shortcut for Pythonwin created The pywin32 extensions were successfully installed.



