missing comtypes module attributes (comtypes.gen.esriFramework.AppRef)

8175
6
02-20-2015 12:15 PM
KerryAlley
Occasional Contributor

I am using comtypes and Mark Cederholm's Snippets.py (10.2 version) and have not been able to access comtypes.gen.esriFramework.AppRef.  I have made the required comtype edits for 10.2 compatibility, and verified that the relevant .pyc files are new.

The error message I get when I try to access comtypes.gen.esriFramework.AppRef is "AttributeError: 'module' object has no attribute 'AppRef'".

The attributes listed by executing "dir(comtypes.gen.esriFramework)" are:  ['_866AE5D3_530C_11D2_A2BD_0000F8774FB5_0_10_2', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'typelib_path'] which does not include "AppRef", although the Class "AppRef" is in the code of the freshly created comtypes\gen\_866AE5D3_530C_11D2_A2BD_0000F8774FB5_0_10_2.py

But I also noticed that a class does not seem to show up as an attribute when I execute "dir(comtypes.gen.esriFramework._866AE5D3_530C_11D2_A2BD_0000F8774FB5_0_10_2)" that yields ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'typelib_path']

Any idea what the problem is?  Is the information I provided above relevant to figuring it out?  How else can I troubleshoot this?

0 Kudos
6 Replies
JoshuaBixby
MVP Esteemed Contributor

Posting the exact snippet and error code are helpful.  There are lots of snippets.

How are you running the code, the interactive Python windows in ArcGIS Desktop or as a standalone script?

0 Kudos
KerryAlley
Occasional Contributor

I'm running from the ArcMap Python window.

My code is:

import arcpy, comtypes, Snippets
Snippets.ArcMap_GetSelectedGeometry(bStandalone=False)

The error message:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "V:\Projects\Shared\kalley\PythonScripts\Snippets.py", line 258, in ArcMap_GetSelectedGeometry
    pApp = GetCurrentApp()
  File "V:\Projects\Shared\kalley\PythonScripts\Snippets.py", line 126, in GetCurrentApp
    return NewObj(esriFramework.AppRef, esriFramework.IApplication)
AttributeError: 'module' object has no attribute 'AppRef'

The entire Snippets.py file is in the link in my original post, but the snippets involved in my line of code are:

def GetLibPath():
    """Return location of ArcGIS type libraries as string"""
    # This will still work on 64-bit machines because Python runs in 32 bit mode
    import _winreg
    keyESRI = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\ESRI\\Desktop10.2")
    return _winreg.QueryValueEx(keyESRI, "InstallDir")[0] + "com\\"

def GetModule(sModuleName):
    """Import ArcGIS module"""
    from comtypes.client import GetModule
    sLibPath = GetLibPath()
    GetModule(sLibPath + sModuleName)

def GetDesktopModules():
    """Import basic ArcGIS Desktop libraries"""
    GetModule("esriFramework.olb")
    GetModule("esriArcMapUI.olb")
    GetModule("esriArcCatalogUI.olb")

#**** Helper Functions ****

def NewObj(MyClass, MyInterface):
    """Creates a new comtypes POINTER object where\n\
    MyClass is the class to be instantiated,\n\
    MyInterface is the interface to be assigned"""
    from comtypes.client import CreateObject
    try:
        ptr = CreateObject(MyClass, interface=MyInterface)
        return ptr
    except:
        return None

def GetCurrentApp():
    """Gets an IApplication handle to the current app.\n\
    Must be run inside the app's Python window.\n\
    Execute GetDesktopModules() first"""
    import comtypes.gen.esriFramework as esriFramework
    return NewObj(esriFramework.AppRef, esriFramework.IApplication)

def ArcMap_GetSelectedGeometry(bStandalone=False):

    GetDesktopModules()
    if bStandalone:
        InitStandalone()
        pApp = GetApp()
    else:
        pApp = GetCurrentApp()
    if not pApp:
        print "We found this spoon, sir."
        return
    import comtypes.gen.esriFramework as esriFramework
    import comtypes.gen.esriArcMapUI as esriArcMapUI
    import comtypes.gen.esriSystem as esriSystem
    import comtypes.gen.esriCarto as esriCarto
    import comtypes.gen.esriGeoDatabase as esriGeoDatabase
    import comtypes.gen.esriGeometry as esriGeometry

    # Get selected feature geometry

    pDoc = pApp.Document
    pMxDoc = CType(pDoc, esriArcMapUI.IMxDocument)
    pMap = pMxDoc.FocusMap
    pFeatSel = pMap.FeatureSelection
    pEnumFeat = CType(pFeatSel, esriGeoDatabase.IEnumFeature)
    pEnumFeat.Reset()
    pFeat = pEnumFeat.Next()
    if not pFeat:
        print "No selection found."
        return
    pShape = pFeat.ShapeCopy
    eType = pShape.GeometryType
    if eType == esriGeometry.esriGeometryPoint:
        print "Geometry type = Point"
    elif eType == esriGeometry.esriGeometryPolyline:
        print "Geometry type = Line"
    elif eType == esriGeometry.esriGeometryPolygon:
        print "Geometry type = Poly"
    else:
        print "Geometry type = Other"
    return pShape
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Now I remember why this sounded so familiar, I ran into the same issue about six months back.  Turn off "Background Processing."  I am guessing you have it enabled, which means some of the code is being run out of process, so there is no AppRef.

0 Kudos
KerryAlley
Occasional Contributor

Alas, I do not have Background Processing enabled.  Any other ideas?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Huh, not sure then.  For me, Background geoprocessing generates that exact error and disabling it makes it go away using the code above.  Maybe close out of ArcGIS Desktop and clean out all of the PYC and PYO files in the comtypes site package folder as well as clear out the gen folder under comtypes.

KerryAlley
Occasional Contributor

I had already deleted all the .pyc and .pyo files from comtypes/gen.  At your suggestion I also deleted them from the comtypes folder, but I still had errors (I forget which).  I uninstalled and reinstalled comtypes-0.6.2 (from sourceforge) and that seems to have fixed everything!  Sometimes a fresh start is easier than figuring out what went wrong.

0 Kudos