comtypes
comtypes copied to clipboard
NameError error
I have the code like this:
import comtypes
from comtypes import *
from comtypes.client import *
comtypes.client.GetModule('UIAutomationCore.dll')
from comtypes.gen.UIAutomationClient import *
uia = CreateObject(CUIAutomation._reg_clsid_, interface=IUIAutomation)
from pywinauto.application import Application
app = Application(backend="uia").start("notepad.exe")
app.UntitledNotepad.type_keys("%FX")
when I run it with Python 3.8.7(comtypes version 1.1.12) , it appears to show me the error, I don't know how to fix it:
C:\stock\program>python robot.py
Traceback (most recent call last):
File "robot.py", line 5, in <module>
comtypes.client.GetModule('UIAutomationCore.dll')
File "C:\Users\zbool\AppData\Roaming\Python\Python38\site-packages\comtypes\client\_generate.py", line 159, in GetModule
mod = _CreateWrapper(tlib, pathname)
File "C:\Users\zbool\AppData\Roaming\Python\Python38\site-packages\comtypes\client\_generate.py", line 227, in _CreateWrapper
generate_module(tlib, ofi, pathname)
File "C:\Users\zbool\AppData\Roaming\Python\Python38\site-packages\comtypes\tools\tlbparser.py", line 767, in generate_module
gen.generate_code(list(items.values()), filename=pathname)
File "C:\Users\zbool\AppData\Roaming\Python\Python38\site-packages\comtypes\tools\codegenerator.py", line 290, in generate_code
self.generate_all(items)
File "C:\Users\zbool\AppData\Roaming\Python\Python38\site-packages\comtypes\tools\codegenerator.py", line 206, in generate_all
self.generate(item)
File "C:\Users\zbool\AppData\Roaming\Python\Python38\site-packages\comtypes\tools\codegenerator.py", line 202, in generate
mth(item)
File "C:\Users\zbool\AppData\Roaming\Python\Python38\site-packages\comtypes\tools\codegenerator.py", line 882, in ComInterface
self.generate(itf.get_head())
File "C:\Users\zbool\AppData\Roaming\Python\Python38\site-packages\comtypes\tools\codegenerator.py", line 202, in generate
mth(item)
File "C:\Users\zbool\AppData\Roaming\Python\Python38\site-packages\comtypes\tools\codegenerator.py", line 903, in ComInterfaceHead
self.generate(base.get_head())
File "C:\Users\zbool\AppData\Roaming\Python\Python38\site-packages\comtypes\tools\codegenerator.py", line 202, in generate
mth(item)
File "C:\Users\zbool\AppData\Roaming\Python\Python38\site-packages\comtypes\tools\codegenerator.py", line 798, in External
comtypes.client.GetModule(ext.tlib)
File "C:\Users\zbool\AppData\Roaming\Python\Python38\site-packages\comtypes\client\_generate.py", line 159, in GetModule
mod = _CreateWrapper(tlib, pathname)
File "C:\Users\zbool\AppData\Roaming\Python\Python38\site-packages\comtypes\client\_generate.py", line 242, in _CreateWrapper
mod = _my_import(fullname)
File "C:\Users\zbool\AppData\Roaming\Python\Python38\site-packages\comtypes\client\_generate.py", line 26, in _my_import
return __import__(fullname, globals(), locals(), ['DUMMY'])
File "C:\Users\zbool\AppData\Roaming\Python\Python38\site-packages\comtypes\gen\_00020430_0000_0000_C000_000000000046_0_2_0.py", line 19, in <module>
OLE_YPOS_PIXELS = c_int
NameError: name 'c_int' is not defined
me too pthon 2.7.18 comtypes 1.1.12
+1
Reverting to comtypes 1.1.11 fixes for me
Provably, module under comtypes/gen/... generated by comtypes/tools/codegenerator.py has not imported c_int from ctypes.

Python 3.9.1 comtypes 1.1.12
Thank you guys! I've removed 1.1.12 from PyPI for now. Next release will be 1.1.13 with the right fix. Sorry for breaking everything.
I have a similar issue but with c_float
comtypes\gen\_00020430_0000_0000_C000_000000000046_0_2_0.py", line 18, in <module>
OLE_XPOS_CONTAINER = c_float
NameError: name 'c_float' is not defined
The broken part is provably below. https://github.com/enthought/comtypes/blob/0094a55ae3d80fc115b51ace9ae274263ac570e9/comtypes/tools/codegenerator.py#L329-L335
"import part" in generated module is made out of self.imports dict iteration.
https://github.com/enthought/comtypes/blob/0094a55ae3d80fc115b51ace9ae274263ac570e9/comtypes/tools/codegenerator.py#L296-L302
Thus, self.imports must be completed before the iteration.
But self.imports['*'] = 'ctypes' was called after the iteration.
I PRed #287 to fix this error.
@zboolk 1.1.13 (and more further version) that resolves this issue is out. Please try.
Works for me now in the new 1.1.13 and 1.1.14.
