Py2Exe error caused by pywinauto on some systems: "AttributeError: 'module' object has no attribute 'CUIAutomation"

Py2Exe error caused by pywinauto on some systems: "AttributeError: 'module' object has no attribute 'CUIAutomation"

我正在向多个 Windows 2012 R2 推出一个 EXE,其中大多数都运行良好。然而,看似在相同的条件下,我有少数吐出错误: AttributeError: 'module' object has no attribute 'CUIAutomation'

我曾经只有:

from pywinauto.findwindows import find_window
from pywinauto.win32functions import SetForegroundWindow

我还尝试添加:

import pywinauto

但这并没有起到多大作用。我还尝试更改代码,所以我没有使用 find_window,而是使用了完整的 pywinauto.findwindowsSetForegroundWindow.

相同

我的进口清单。草率或多余的请见谅,我是新人,正在自学。

import requests
import json
import os
import winshell
import sys
import re
import subprocess
import win32gui
import win32api
import win32con
import os.path
import ctypes
import fnmatch
import urllib2
import shutil
import psutil
import pywinauto
from pywinauto.findwindows import find_window
from pywinauto.win32functions import SetForegroundWindow
from pathlib import Path
from win32com.client import Dispatch
from stat import S_IREAD, S_IRGRP, S_IROTH
from time import sleep
from bs4 import BeautifulSoup

我在脚本中唯一使用 pywinauto 的地方是:

SetForegroundWindow(find_window(title_re = r'Server*'))

某些系统启动时出现 py2exe 错误:

File "SVMon.py", line 17, in <module>
File "c:\users\administrator\appdata\local\temp\pip-install-dm1p2f\PyInstaller\PyInstaller\loader\pyimod03_importers.py", line 395, in load_module
File "site-packages\pywinauto\__init__.py", line 89, in <module>
File "c:\users\administrator\appdata\local\temp\pip-install-dm1p2f\PyInstaller\PyInstaller\loader\pyimod03_importers.py", line 395, in load_module
File "site-packages\pywinauto\findwindows.py", line 42, in <module>
File "c:\users\administrator\appdata\local\temp\pip-install-dm1p2f\PyInstaller\PyInstaller\loader\pyimod03_importers.py", line 395, in load_module
File "site-packages\pywinauto\controls\__init__.py", line 36, in <module>
File "c:\users\administrator\appdata\local\temp\pip-install-dm1p2f\PyInstaller\PyInstaller\loader\pyimod03_importers.py", line 395, in load_module
File "site-packages\pywinauto\controls\uiawrapper.py", line 47, in <module>
File "c:\users\administrator\appdata\local\temp\pip-install-dm1p2f\PyInstaller\PyInstaller\loader\pyimod03_importers.py", line 395, in load_module
File "site-packages\pywinauto\uia_defines.py", line 181, in <module>
File "site-packages\pywinauto\uia_defines.py", line 169, in _build_pattern_ids_dic
File "site-packages\pywinauto\uia_defines.py", line 50, in __call__
File "site-packages\pywinauto\uia_defines.py", line 63, in __init__
AttributeError: 'module' object has no attribute 'CUIAutomation'
[1928] Failed to execute script SVMon

我最终使用 PyInstaller 解决了这个问题,我不确定是什么导致了这个问题,但我添加了我能想到的所有隐藏导入:

pyinstaller --hidden-import comtypes.gen.UIAutomationClient --hidden-import comtypes --hidden-import comtypes.gen --hidden-import comtypes.patcher --hidden-import comtypes.GUID --hidden-import pywinauto --hidden-import pywinauto.CUIAutomation --hidden-import pywinauto.findwindows --hidden-import pywinauto.findwindows VPSMonitor.py --onefile --clean

升级 pip install comtype=1.1.7 它会工作 - 如果不降级并再次升级到最新版本