[GIS] Debugging ‘module’ object has no attribute ‘SdeWorkspaceFactory’

arcgis-10.1arcobjectscomtypesenterprise-geodatabasepython

I am new to python, am debugging one of existing python code, where am facing an error "'module' object has no attribute 'SdeWorkspaceFactory". Earlier this code was working fine but suddenly does not work.

am using sde connection file from path
"C:\Users\user1\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\Connection.sde

I could able to connect this using sde file using Arcatalog.

Environment : ArcGIS 10.1

I cannot provide entire code base, however most of code looks like correct.

Please provide me a pointer?

def OpenFeatureClass(sWorkspace, sFCName):

    import comtypes.gen.esriGeoDatabase as esriGeoDatabase
    import comtypes.gen.esriDataSourcesGDB as esriDataSourcesGDB

    #pWSF = NewObj(esriDataSourcesGDB.FileGDBWorkspaceFactory, esriGeoDatabase.IWorkspaceFactory)
    pWSF = NewObj(esriDataSourcesGDB.SdeWorkspaceFactory, esriGeoDatabase.IWorkspaceFactory)
    pWS = pWSF.OpenFromFile(sWorkspace, 0)
    pFWS = CType(pWS, esriGeoDatabase.IFeatureWorkspace)
    pFC = pFWS.OpenFeatureClass(sFCName)
    return pFC

Best Answer

You may get better help by changing the tags from arcpy to ArcObjects as this has nothing to do with arcpy, you are accessing ArcObjects using python's comtypes module.

Regarding your error, you may need to reload all your ArcObjects Object Library files (*.olb) files. I was able to successfully get the IWorkspaceFactory2 pointer from your code:

enter image description here

See if reloading the ArcObjects libraries helps.

EDIT: The object libraries need to updated any time you change versions in ArcGIS. The safe thing is to go into your comtypes "gen" folder and delete everything but the "init.py" script and run the load all function (in below code). Here are some helper functions:

import os
import glob

def load_all():
    '''loads all object libraries'''
    from comtypes.client import GetModule
    mods = glob.glob(os.path.join(GetLibPath(), '*.olb'))
    for mod in mods:
        GetModule(mod)
    return

def InstallInfo():
    """Gets ArcGIS Install Info"""
    # Get ArcObjects version
    import comtypes
    from comtypes.client import GetModule
    g = comtypes.GUID("{6FCCEDE0-179D-4D12-B586-58C88D26CA78}")
    GetModule((g, 1, 0))
    import comtypes.gen.ArcGISVersionLib as esriVersion
    pVM = NewObj(esriVersion.VersionManager, esriVersion.IArcGISVersion)
    return pVM.GetVersions().Next()

def GetLibPath():
    '''Reference to com directory which houses ArcObjects
    Ojbect Libraries (*.OLB)'''
    return os.path.join(InstallInfo()[2], 'com')

def GetVersion():
    """returns ArcGIS Version"""
    return InstallInfo()[1]

def getModule(sModuleName):
    ''' loads the object library by name'''
    from comtypes.client import GetModule
    olb = os.path.abspath(os.path.join(GetLibPath(), sModuleName))
    return GetModule(olb)