pyinstaller-hooks-contrib icon indicating copy to clipboard operation
pyinstaller-hooks-contrib copied to clipboard

PyInstaller 3.3.1 does not work with Pywinauto lib import

Open korniichuk opened this issue 6 years ago • 15 comments

Hi all,

need help with PyInstaller and Pywinauto lib.

test.py file (one liner):

from pywinauto.application import Application

Create EXE file based on test.py file:

> pyinstaller test.py

PyInstaller output:

201 INFO: PyInstaller: 3.3.1
202 INFO: Python: 3.6.4
202 INFO: Platform: Windows-7-6.1.7601-SP1
203 INFO: wrote D:\test.spec
205 INFO: UPX is not available.
208 INFO: Extending PYTHONPATH with paths
['D:\\', 'D:\\']
208 INFO: checking Analysis
209 INFO: Building Analysis because out00-Analysis.toc is non existent
209 INFO: Initializing module dependency graph...
217 INFO: Initializing module graph hooks...
221 INFO: Analyzing base_library.zip ...
5616 INFO: running Analysis out00-Analysis.toc
5620 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of f
inal executable
  required by d:\users\rkorniic\appdata\local\programs\python\python36-32\python
.exe
6620 INFO: Caching module hooks...
6629 INFO: Analyzing D:\test.py
9430 INFO: Processing pre-find module path hook   distutils
11349 INFO: Processing pre-find module path hook   site
11350 INFO: site: retargeting to fake-dir 'd:\\users\\rkorniic\\appdata\\local\\
programs\\python\\python36-32\\lib\\site-packages\\PyInstaller\\fake-modules'
11412 INFO: Processing pre-safe import module hook   win32com
14395 INFO: Loading module hooks...
14395 INFO: Loading module hook "hook-certifi.py"...
14400 INFO: Loading module hook "hook-distutils.py"...
14405 INFO: Loading module hook "hook-encodings.py"...
14653 INFO: Loading module hook "hook-lib2to3.py"...
14673 INFO: Loading module hook "hook-PIL.Image.py"...
15448 INFO: Loading module hook "hook-PIL.py"...
15449 INFO: Loading module hook "hook-PIL.SpiderImagePlugin.py"...
15450 INFO: Loading module hook "hook-pkg_resources.py"...
16741 INFO: Loading module hook "hook-pycparser.py"...
17094 INFO: Loading module hook "hook-pydoc.py"...
17096 INFO: Loading module hook "hook-pythoncom.py"...
17829 INFO: Loading module hook "hook-pywintypes.py"...
18552 INFO: Loading module hook "hook-setuptools.py"...
18592 INFO: Loading module hook "hook-sysconfig.py"...
18594 INFO: Loading module hook "hook-win32com.py"...
18728 INFO: Loading module hook "hook-xml.etree.cElementTree.py"...
18729 INFO: Loading module hook "hook-xml.py"...
19103 INFO: Loading module hook "hook-_tkinter.py"...
19726 INFO: checking Tree
19726 INFO: Building Tree because out00-Tree.toc is non existent
19726 INFO: Building Tree out00-Tree.toc
19945 INFO: checking Tree
19946 INFO: Building Tree because out01-Tree.toc is non existent
19946 INFO: Building Tree out01-Tree.toc
20045 INFO: Looking for ctypes DLLs
20063 WARNING: library Shcore.dll required via ctypes not found
20464 WARNING: library coredll required via ctypes not found
20620 INFO: Analyzing run-time hooks ...
20628 INFO: Including run-time hook 'pyi_rth_win32comgenpy.py'
20632 INFO: Including run-time hook 'pyi_rth__tkinter.py'
20635 INFO: Including run-time hook 'pyi_rth_pkgres.py'
20638 INFO: Including run-time hook 'pyi_rth_multiprocessing.py'
20658 INFO: Looking for dynamic libraries
22051 INFO: Looking for eggs
22051 INFO: Using Python library d:\users\rkorniic\appdata\local\programs\python
\python36-32\python36.dll
22052 INFO: Found binding redirects:
[]
22060 INFO: Warnings written to D:\build\test\warntest.txt
22247 INFO: Graph cross-reference written to D:\build\test\xref-test.html
22341 INFO: checking PYZ
22341 INFO: Building PYZ because out00-PYZ.toc is non existent
22341 INFO: Building PYZ (ZlibArchive) D:\build\test\out00-PYZ.pyz
24150 INFO: Building PYZ (ZlibArchive) D:\build\test\out00-PYZ.pyz completed suc
cessfully.
24185 INFO: checking PKG
24185 INFO: Building PKG because out00-PKG.toc is non existent
24185 INFO: Building PKG (CArchive) out00-PKG.pkg
24221 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
24223 INFO: Bootloader d:\users\rkorniic\appdata\local\programs\python\python36-
32\lib\site-packages\PyInstaller\bootloader\Windows-32bit\run.exe
24223 INFO: checking EXE
24224 INFO: Building EXE because out00-EXE.toc is non existent
24224 INFO: Building EXE from out00-EXE.toc
24226 INFO: Appending archive to EXE D:\build\test\test.exe
24234 INFO: Building EXE from out00-EXE.toc completed successfully.
24242 INFO: checking COLLECT
24243 INFO: Building COLLECT because out00-COLLECT.toc is non existent
24244 INFO: Building COLLECT out00-COLLECT.toc
27043 INFO: Building COLLECT out00-COLLECT.toc completed successfully.

Run created test.exe file. Output:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "d:\users\rkorniic\appdata\local\programs\python\python36-32\lib\site-pac
kages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\pywinauto\__init__.py", line 40, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "d:\users\rkorniic\appdata\local\programs\python\python36-32\lib\site-pac
kages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\pywinauto\findwindows.py", line 42, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "d:\users\rkorniic\appdata\local\programs\python\python36-32\lib\site-pac
kages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\pywinauto\controls\__init__.py", line 36, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "d:\users\rkorniic\appdata\local\programs\python\python36-32\lib\site-pac
kages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\pywinauto\controls\uiawrapper.py", line 44, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "d:\users\rkorniic\appdata\local\programs\python\python36-32\lib\site-pac
kages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\pywinauto\uia_defines.py", line 175, in <module>
  File "site-packages\pywinauto\uia_defines.py", line 163, in _build_pattern_ids
_dic
  File "site-packages\pywinauto\uia_defines.py", line 50, in __call__
  File "site-packages\pywinauto\uia_defines.py", line 60, in __init__
  File "site-packages\comtypes\client\_generate.py", line 110, in GetModule
  File "site-packages\comtypes\client\_generate.py", line 169, in _CreateWrapper

FileNotFoundError: [Errno 2] No such file or directory: 'D:\\dist\\test\\comtype
s\\gen\\_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py'
[6308] Failed to execute script test

So, error is:

FileNotFoundError: [Errno 2] No such file or directory: 'D:\\dist\\test\\comtype
s\\gen\\_944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py'
[6308] Failed to execute script test

test.spec file:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['test.py'],
             pathex=['D:\\'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='test',
          debug=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='test')

Environment:

  • Windows 7 Professional. Service Pack 1
  • Python 3.6.4
  • pyinstaller 3.3.1
  • pywinauto 0.6.3
  • comtypes 1.1.4

korniichuk avatar Jan 04 '18 14:01 korniichuk

I have the same environment, at least for the packages listed above. I first use virtual environment and get the same error. Then I switch back the original environment and get my exe work. Are you using anacondas? May be add ” --hidden-import=comtypes “ in the setup file. An other strange thing is if I switch back to the virtual environment but keep the same pycache folder, my script also works.

yu45020 avatar Jan 15 '18 10:01 yu45020

I have the same problem, and I am not using anacondas (I don't know what it is). I tried "--hidden-import=comtypes" and it does not help.

Environment: Windows 10 Python 2.7.14 PyInstaller 3.3.1 pywinauto 0.6.3 comtypes 1.1.4

Thank you very much for any help!

luziling avatar Jan 15 '18 10:01 luziling

Anacondas contains lots of common packages with Python. I use it to build exe, and I always get it work, thought the size is really LARGE ( usually include pyqt, numpy,mkl , etc). I have no knowledge on tech details, but I guess when a program gets built, python will write some compiled files, including comtype, in pycache (not generated by pyinstaller). Then when you build the second time, pyinstaller will find the codes.

yu45020 avatar Jan 15 '18 10:01 yu45020

@yu45020 thanks for your comment. PyInstaller does not work with Pywinauto. Python 3.6.4 and Python 2.7.14. With Anaconda and w/o Anaconda. With and w/o --hidden-import=comtypes. py2exe works with Pywinauto and Python 2.7.14 as well, but I can not create a one-file bundled executable with py2exe.

korniichuk avatar Jan 15 '18 11:01 korniichuk

This looks like File "site-packages\comtypes\client_generate.py", line 169, in _CreateWrapper tries to open a file (namely _944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py. Please check this place to learn what is done there and why this might fail.

Please check if py2exe includes special treatment for pyautowin.

htgoebel avatar Jan 15 '18 21:01 htgoebel

Just now I fixed this @korniichuk

Create folder comtypes/gen/ in the same folder of your test.py, and copy file _944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py and UIAutomationClient.py (under Lib/site-packages/comtypes/gen/) into this folder.

Then use pyinstaller --hidden-import comtypes.gen._944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0 --hidden-import comtypes.gen.UIAutomationClient test.py

Good luck

luziling avatar Jan 16 '18 03:01 luziling

@luziling many thanks!!

Note: This is work for me pyinstaller --hidden-import comtypes.gen._944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0 --hidden-import comtypes.gen.UIAutomationClient test.py --onefile --clean

But this is not work for me pyinstaller --onefile --clean --hidden-import comtypes.gen._944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0 --hidden-import comtypes.gen.UIAutomationClient test.py

Different is --onefile and --clean argument place. Maybe, first place of argument --clean is remove required files.

ghost avatar Feb 21 '18 13:02 ghost

I've got a an additional issue with pywinauto and pyinstaller:

Traceback (most recent call last):
  File "main.py", line 19, in <module>
    from pywinauto import ElementNotFoundError, win32defines, MatchError, ElementAmbiguousError, win32functions
  File "c:\users\panni\appdata\local\temp\pip-build-thtnju\pyinstaller\PyInstaller\loader\pyimod03_importers.py", line 396, in load_module
  File "site-packages\pywinauto\__init__.py", line 72, in <module>
  File "c:\users\panni\appdata\local\temp\pip-build-thtnju\pyinstaller\PyInstaller\loader\pyimod03_importers.py", line 396, in load_module
  File "site-packages\pywinauto\findwindows.py", line 42, in <module>
  File "c:\users\panni\appdata\local\temp\pip-build-thtnju\pyinstaller\PyInstaller\loader\pyimod03_importers.py", line 396, in load_module
  File "site-packages\pywinauto\controls\__init__.py", line 36, in <module>
  File "c:\users\panni\appdata\local\temp\pip-build-thtnju\pyinstaller\PyInstaller\loader\pyimod03_importers.py", line 396, in load_module
  File "site-packages\pywinauto\controls\uiawrapper.py", line 46, in <module>
  File "c:\users\panni\appdata\local\temp\pip-build-thtnju\pyinstaller\PyInstaller\loader\pyimod03_importers.py", line 396, 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 60, in __init__
  File "site-packages\comtypes\client\_generate.py", line 110, in GetModule
  File "site-packages\comtypes\client\_generate.py", line 172, in _CreateWrapper
  File "site-packages\comtypes\tools\tlbparser.py", line 750, in generate_module
  File "site-packages\comtypes\tools\codegenerator.py", line 241, in generate_code
  File "site-packages\comtypes\tools\codegenerator.py", line 189, in generate_all
  File "site-packages\comtypes\tools\codegenerator.py", line 185, in generate
  File "site-packages\comtypes\tools\codegenerator.py", line 704, in ComInterface
  File "site-packages\comtypes\tools\codegenerator.py", line 185, in generate
  File "site-packages\comtypes\tools\codegenerator.py", line 725, in ComInterfaceHead
  File "site-packages\comtypes\tools\codegenerator.py", line 185, in generate
  File "site-packages\comtypes\tools\codegenerator.py", line 631, in External
  File "site-packages\comtypes\client\_generate.py", line 110, in GetModule
  File "site-packages\comtypes\client\_generate.py", line 184, in _CreateWrapper
  File "site-packages\comtypes\client\_generate.py", line 24, in _my_import
ImportError: No module named _00020430_0000_0000_C000_000000000046_0_2_0
[20380] Failed to execute script main

_00020430_0000_0000_C000_000000000046_0_2_0.py included in comtypes\gen and besides the main.py.

Windows 10 Pro, newest update.

pannal avatar Mar 08 '18 06:03 pannal

@luziling no need to create folders or copy files (however I only use the win32 backend)

pyinstaller --hidden-import comtypes.gen._944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0 --hidden-import comtypes.gen.UIAutomationClient main.py --onefile

PyInstaller 3.3.1 Python 3.6.5 pywinauto 0.6.4

meshuggahtas avatar Jun 19 '18 11:06 meshuggahtas

I've recently had an issue similar to this. After much trial-and-error, I fixed it by adding these to my pyinstaller command line:

--hidden-import comtypes
--hidden-import comtypes.gen
--hidden-import comtypes.patcher
--hidden-import comtypes.GUID

morphine00 avatar Oct 18 '18 15:10 morphine00

My application throwed a different error:

AttributeError: module 'comtypes.gen.UIAutomationClient' has no attribute 'CUIAutomation'

It turned out that comtypes.gen package is not present initially but generated only when requested. This caused error when building in a fresh virtual env. The solution is:

python -c "import pywinauto"
pyinstaller ... --hidden-import=comtypes.gen.UIAutomationClient ...

(python 3.6.8, pywinauto 0.6.6, comtypes 1.1.7, pyinstaller 3.4)

xuhcc avatar Apr 25 '19 09:04 xuhcc

@luziling any possible drawbacks on this solution that you can foresee? My main concern would be freezing it on windows 10, but then it not working on another version of windows.

hansonap avatar Dec 10 '20 18:12 hansonap

Hi , has anyone got this as the error: import pywinauto.application File "", line 991, in _find_and_load File "", line 975, in _find_and_load_unlocked File "", line 671, in load_unlocked File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module File "pywinauto_init.py", line 87, in File "", line 991, in _find_and_load File "", line 975, in _find_and_load_unlocked File "", line 671, in _load_unlocked File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module File "pywinauto\sysinfo.py", line 44, in File "", line 991, in _find_and_load File "", line 975, in find_and_load_unlocked File "", line 671, in load_unlocked File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module File "comtypes_init.py", line 160, in File "comtypes_init.py", line 151, in CoInitializeEx File "_ctypes/callproc.c", line 935, in GetResult OSError: [WinError -2147417850] Cannot change thread mode after it is set

GoelPri avatar Nov 02 '21 12:11 GoelPri

@GoelPri Open a new issue and fill out the bug report template. That looks unreleated.

bwoodsend avatar Nov 02 '21 20:11 bwoodsend

@GoelPri Open a new issue and fill out the bug report template. That looks unreleated.

Sure

GoelPri avatar Nov 04 '21 05:11 GoelPri