comtypes icon indicating copy to clipboard operation
comtypes copied to clipboard

NameError error

Open zboolk opened this issue 3 years ago • 10 comments
trafficstars

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

zboolk avatar Mar 05 '22 17:03 zboolk

me too pthon 2.7.18 comtypes 1.1.12

haohetao avatar Mar 07 '22 01:03 haohetao

+1

Reverting to comtypes 1.1.11 fixes for me

Filthy999 avatar Mar 07 '22 04:03 Filthy999

Provably, module under comtypes/gen/... generated by comtypes/tools/codegenerator.py has not imported c_int from ctypes.

image

Python 3.9.1 comtypes 1.1.12

junkmd avatar Mar 07 '22 07:03 junkmd

is if tlib_mtime is not None: in comtypes/tools/codegenerator.py correct?

image

junkmd avatar Mar 07 '22 07:03 junkmd

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.

vasily-v-ryabov avatar Mar 07 '22 07:03 vasily-v-ryabov

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

PhilippSelenium avatar Mar 08 '22 06:03 PhilippSelenium

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.

junkmd avatar Mar 17 '22 22:03 junkmd

I PRed #287 to fix this error.

junkmd avatar Mar 18 '22 01:03 junkmd

@zboolk 1.1.13 (and more further version) that resolves this issue is out. Please try.

junkmd avatar Aug 23 '22 14:08 junkmd

Works for me now in the new 1.1.13 and 1.1.14.

jonschz avatar Sep 21 '22 05:09 jonschz