1

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
  • CType is not a python instruction (that I know of). You are looking at C# and trying to implement it in python. You would be better to create a simple model and export to python to see how Esri opens SDE data - it's far simpler than that!! It would be something like "Database Connections\\Connection\\sFCName". Even if it did work pFC is returned as a COM IFeatureClass object which is not compatible with arcpy geoprocessing. Excellent question nonetheless. – Michael Stimson Feb 17 '15 at 22:58
  • @MichaelMiles-Stimson, I believe he is calling a common function from the Snippets module. pierssen.com/arcgis10/upload/python/snippets102.py – crmackey Feb 17 '15 at 23:18
  • 1
    However, I agree with Michael, why not use arcpy? It is much easier than fiddling with ArcObjects in Python. Can be very tricky/buggy. – crmackey Feb 17 '15 at 23:20
3

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)
  • Thanks. How to reload all "*.olb" files. In program - i have below lines GetModule(sLibPath + "esriGeoDatabase.olb") GetModule(sLibPath + "esriDataSourcesGDB.olb") – iamlaksh1 Feb 17 '15 at 21:33
  • I edited my answer to add some more details. – crmackey Feb 17 '15 at 22:56
  • I see some strange behavior, when I execute the script in another machine,it throws an error 'module' object has no attribute 'SdeWorkspaceFactory'. Whereas in another machine, it works fine. ArcGIS versions are same. Any clue? worskspace is set correctly. – iamlaksh1 Feb 18 '15 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.