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

倖福魔咒の 提交于 2021-02-10 14:16:27

问题


I'm rolling out an EXE to several Windows 2012 R2's and the majority of them work just fine. However, seemingly under the same condition, I have a handful that spit out the error: AttributeError: 'module' object has no attribute 'CUIAutomation'

I used to just have:

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

And I tried also adding in:

import pywinauto

But that didn't do much. I also attempted to change the code so instead of using find_window I used the full pywinauto.findwindows. Same for SetForegroundWindow.

My import list. Sorry if it's sloppy or redundant, I'm new and self-learning.

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

Really the only place I'm using pywinauto in my script is:

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

py2exe error on launch for some systems:

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

回答1:


I ended up fixing this by using PyInstaller and I'm not exactly sure what did the trick but I added every hidden import I could think of:

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



回答2:


Upgrade pip install comtype=1.1.7 It will work- if not downgrade it and again upgrade it to the latest one



来源:https://stackoverflow.com/questions/56910153/py2exe-error-caused-by-pywinauto-on-some-systems-attributeerror-module-obje

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!