无法在Windows 10上导入pywinauto

12

我使用 pip install pywinauto 命令安装了 pywinauto 库。

操作系统:Windows 10

Python 版本:3.6.2

当我运行 python 并尝试导入 pywinauto 时,出现以下错误:

Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pywinauto.application import Application
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\pywinauto\__init__.py", line 89, in <module>
    from . import findwindows
  File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\pywinauto\findwindows.py", line 42, in <module>
    from . import controls
  File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\pywinauto\controls\__init__.py", line 36, in <module>
    from . import uiawrapper # register "uia" back-end (at the end of uiawrapper module)
  File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\pywinauto\controls\uiawrapper.py", line 47, in <module>
    from ..uia_defines import IUIA
  File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\pywinauto\uia_defines.py", line 181, in <module>
    pattern_ids = _build_pattern_ids_dic()
  File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\pywinauto\uia_defines.py", line 169, in _build_pattern_ids_dic
    if hasattr(IUIA().ui_automation_client, cls_name):
  File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\pywinauto\uia_defines.py", line 50, in __call__
    cls._instances[cls] = super(_Singleton, cls).__call__(*args, **kwargs)
  File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\pywinauto\uia_defines.py", line 60, in __init__
    self.UIA_dll = comtypes.client.GetModule('UIAutomationCore.dll')
  File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\comtypes\client\_generate.py", line 118, in GetModule
    mod = _CreateWrapper(tlib, pathname)
  File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\comtypes\client\_generate.py", line 183, in _CreateWrapper
    generate_module(tlib, ofi, pathname)
  File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\comtypes\tools\tlbparser.py", line 750, in generate_module
    gen.generate_code(list(items.values()), filename=pathname)
  File "C:\Users\John\AppData\Roaming\Python\Python36\site-packages\comtypes\tools\codegenerator.py", line 261, in generate_code
    tlib_mtime = os.stat(self.filename).st_mtime
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'UIAutomationCore.dll'

有什么想法可以解决这个问题吗?


我不知道这是否有所帮助,但值得尝试从互联网下载 UIAutomationCore.dll - Beniamin H
4个回答

24

今天我遇到了同样的问题,通过pip install comtypes==1.1.7进行修复。 这是由comtypes库引起的,该库在12月26日发布了新版本1.1.8。降级到先前的版本后,现在可以正常工作。


3
现在已经在comtypes==1.1.9中解决了。请检查。 - Vasily Ryabov

4

这个问题应该在comtypes==1.1.9中得到解决,请确认您的版本号。

这个问题是由于comtypes==1.1.8引入的,是我的失误。


是的,Windows 的 PATH 变量有点混乱。但是 import pywinauto 在我没有修改 PATH 变量的情况下从无法工作变为可以工作了。要么我安装的其中一个软件修改了 PATH 变量以使其正常工作,要么还有其他原因。 - John Anderson
1
问题出在 comtypes 上,我已经在 comtypes==1.1.9 中修复了它。请检查。 - Vasily Ryabov
1
在 comtypes==1.1.9 中遇到了相同的问题...降级到 1.1.7,问题解决了。 - AbhiGupta
1
相同的问题在1.1.12版本中仍然存在,将其降级到1.1.9版本对我有用。感谢@VasilyRyabov提供的提示。 - Dev
1
@VasilyRyabov,是的 comtypes==1.1.11 也可以,可以解决这个错误。 - Dev
显示剩余4条评论

0

请尝试以下方法,对我来说很有效:

  1. https://github.com/mhammond/pywin32/releases安装pywin32
  2. pip install -U comtypes
  3. pip install six
  4. pip install Pillow
  5. cd c:\windows\syswow64\
  6. regsvr32 UIAutomationCore.dll
  7. python
  8. import pywinauto #这样问题应该就解决了

我使用的是Win10和Python 3.9......我不喜欢Win10。 另一个可能是您的numpy库,卸载它并重新安装。 pip uninstall numpy pip install numpy

注释 -> https://www.dll-files.com/download/65996a0afad353ec43ffe1ca69a2be88/uiautomationcore.dll.html?c=TTM2MGJJaHVRaEJkM1JyNDB3TDNwZz09


在Windows 7上安装Pillow似乎对我有用。 - John S
1
应该在 comtypes==1.1.9 中修复,不需要任何其他技巧。请检查。 - Vasily Ryabov

0

尝试使用不同版本的Python(例如3.7.5或3.8):

https://github.com/pywinauto/pywinauto/issues/887

"

在Python 3.7.6和3.8.1中遇到了同样的问题。但在3.7.5或3.8中没有出现这个问题。 我认为这个问题将在3.8.2中得到解决(但还不确定)。

"

2
我安装了各种Python版本,并在每个版本中执行了 python3.x -m pip install pywinauto。每次安装都成功完成。在每次安装后,我启动相应的Python并尝试 import pywinauto。我安装的版本包括3.7.3、3.7.9、3.8.7和3.9.1(除了原始的3.6.2)。在每种情况下,我都得到了一个错误,就像我在原始帖子中描述的那样。所以我决定发表这个评论,并在这些Python版本中再次运行 import pywinauto。令我完全惊讶的是,原始的3.6.2现在可以工作了!Windows的奥秘! - John Anderson
也许是 comtypes 的新版本问题。可以尝试使用 comtypes==1.1.7。 - Vasily Ryabov
comtypes==1.1.10有更适合Win7和Win10的修复程序,供您参考。 - Vasily Ryabov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接