Select to view content in your preferred language

SearchCursor throws error

1869
10
Jump to solution
07-01-2012 02:20 AM
JoeHershman
MVP Alum
Hi,

I am trying to create a SearchCursor on a table which seems simple enough

    cursor = arcpy.SearchCursor(tablepath)


This line of code though throws an error in _base.py:  global name 'gp_' in not defined.  I cannot seem to find any information on this error.

I can create an UpdateCursor and an InsertCursor without error using the same tablepath variable.  Is there something I am missing in this method call?

I am using ArcGIS 10.0

Thanks
-Joe
Thanks,
-Joe
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Esri Alum
Did you edit any of the arcpy source files? ARe you sure you're using the right version of Python? You may need to reinstall.

View solution in original post

0 Kudos
10 Replies
JoeHershman
MVP Alum
Anyone have any ideas about this?  I basically cannot use a SearchCursor in any script, the same error occurs (as expected) in the Python window.  Why would this only occur on a SearchCursor when everything else is working without issue.
Thanks,
-Joe
0 Kudos
RaphaelR
Deactivated User
does your "tablepath" point to a path or a featureclass/shapefile?
posting the rest of your code might help as well.
0 Kudos
JasonScheirer
Esri Alum
Did you edit any of the arcpy source files? ARe you sure you're using the right version of Python? You may need to reinstall.
0 Kudos
JoeHershman
MVP Alum
Apologizes for not posting a more complete code sample initially


import arcinfo
import arcpy
import sys






def main():
    global mosaicDb
    global pathtable
    pathtable = '%SCRATCHWORKSPACE%/Scratch.gdb/Paths'
    mosaicDb = '//sdm7/arcgis/Mosaics/imagery_datasets.gdb/'


    inputmosaic = arcpy.GetParameterAsText(0)
    objectid = arcpy.GetParameterAsText(1)
    arcpy.AddMessage('Input Mosaic Dataset: ' + mosaicDb + inputmosaic)
    arcpy.AddMessage('Search SourceObjectID: ' + objectid)


    deletePathDb()
    exportCatalog(inputmosaic)
    fs = getMosaicRow(objectid, pathtable)
    arcpy.SetParameter(0, fs)




def deletePathDb():
    try:
        arcpy.Delete_management(pathtable)
        arcpy.AddMessage('table deleted successfully')
    except:
        #catch because will throw error if table not there
        arcpy.AddMessage('Error attempting delete path table')
        arcpy.AddMessage(arcpy.GetMessages())






def exportCatalog(name):
    try:
        fullname = '//sdm7/arcgis/Mosaics/imagery_datasets.gdb/' + name
        arcpy.ExportRasterCatalogPaths_management(fullname, "ALL", pathtable)
    except Exception as e:
        arcpy.AddMessage("Error export catalog")
        arcpy.AddMessage(arcpy.GetMessages())




def getMosaicRow(objectId, table):


    where = 'SourceOID = ' + objectId
#this is line that explodes
    cursor = arcpy.SearchCursor(table, where)
    
    fs = None
    for row in cursor:
        fs = row
    
    return fs
    


As I mentioned above, if I replace SearchCursor with UpdateCursor or InsertCursor it works without error, which is the thing that has my head spinning the most.  I have certainly not consciously modified any of the core .  I am using 10.0 with Python26

Thanks for you help
Thanks,
-Joe
0 Kudos
KimOllivier
Honored Contributor
I am suspicious of your query to open the cursor. Is ObjectID a string? If not it needs to be cast to a string.
What happens if the result is no records?
Add some print statements to print out what is returned.
I have never used the row variable in a cursor, always row.attribute, what does row return?

Update and Insert cursors work differently and since you haven't added a new row it probably just does nothing.
You also need to close the cursor but I suppose that you are relying on the local variables in the function. I would still explicitly 'del cursor'

You may have more success by rearranging your whole script. I would avoid opening and reopening a cursor just to get one record.
Maybe get all the values in one pass and put into a python dictionary? That would be much faster.
0 Kudos
JoeHershman
MVP Alum
I am suspicious of your query to open the cursor. Is ObjectID a string? If not it needs to be cast to a string.
What happens if the result is no records?
Add some print statements to print out what is returned.
I have never used the row variable in a cursor, always row.attribute, what does row return?

Update and Insert cursors work differently and since you haven't added a new row it probably just does nothing.
You also need to close the cursor but I suppose that you are relying on the local variables in the function. I would still explicitly 'del cursor'

You may have more success by rearranging your whole script. I would avoid opening and reopening a cursor just to get one record.
Maybe get all the values in one pass and put into a python dictionary? That would be much faster.


Kimo,

Thanks for the reply, perhaps my initial post was not clear this line throws the error (

global name 'gp_' in not defined)

.


cursor = arcpy.SearchCursor(table, where)



It throws the error if the where clause is not in there and if I just pass the table in (but yes objectId is a string).  The table is created in the

exportCatalog

method so perhaps it has something to do with trying to query a table that was just created. I cannot put in statements to see what is returned because it blows up on that call


The purpose of the script is to retrieve only the one value (at this point), so it is not running multiple times to retrieve multiple values.  I am quite inexperienced with python and geoprocessing so I am sure I am not doing this in the most efficient manner but right now I am just trying to get what seems a pretty straightforward script to work as a geopocessing task

Cheers
-Joe
Thanks,
-Joe
0 Kudos
KimOllivier
Honored Contributor
Ok, I have edited your script to work in Pythonwin in Desktop to debug the script. I see that you are using ArcServer attributes, which is not a useful place to debug a script. I suspected that you had not installed python on the server, but if you get it to start, then that cannot be the case.

# testbug.py
import arcinfo
import arcpy
import sys

def main():
    ##    global mosaicDb
    ##    global pathtable
    # only a geoprocessing service can do this:
    # pathtable = '%SCRATCHWORKSPACE%/Scratch.gdb/Paths'
    # Desktop equivalent 9.3+
    installTemp = arcpy.GetSystemEnvironment("ARCTEMPDIR")
    arcpy.env.scratchWorkspace = installTemp+"/scratch.gdb"
    if not arcpy.Exists(arcpy.env.scratchWorkspace):
        print "failed to set scratch workspace"
        sys.exit()
    pathtable = arcpy.env.scratchWorkspace +"/Paths"
    print pathtable
    mosaicDb = r'D:\data\aerial\catalog\nzmgcat.gdb' # '//sdm7/arcgis/Mosaics/imagery_datasets.gdb/'

    ##    inputmosaic = arcpy.GetParameterAsText(0)
    ##    objectid = arcpy.GetParameterAsText(1)
    inputmosaic = 'nztm1'
    objectid = '40'
    arcpy.AddMessage('Input Mosaic Dataset: ' + mosaicDb + "/" + inputmosaic) # bug fix added separator
    arcpy.AddMessage('Search SourceObjectID: ' + objectid)
    print 'Input Mosaic Dataset: ' + mosaicDb + "/" + inputmosaic
    print 'Search SourceObjectID: ' + objectid
    deletePathDb(pathtable)
    exportCatalog(inputmosaic,pathtable,mosaicDb)
    fs = getMosaicRow(objectid, pathtable)
    print fs
    arcpy.SetParameter(3, fs) # Not 0, this would have to be at least the third parameter

def deletePathDb(pathtable):
    try:
        if arcpy.Exists(pathtable):
            arcpy.Delete_management(pathtable)
            arcpy.AddMessage('table deleted successfully')
            print 'pathtable deleted successfully'
        else :
            print 'pathtable does not exist'
    except:
        #catch because will throw error if table not there
        print "error in deletePathDb"
        arcpy.AddMessage('Error attempting delete path table')
        arcpy.AddMessage(arcpy.GetMessages())

def exportCatalog(name,pathtable,mosaicDb):
    try:
        fullname = mosaicDb + '/' + name
        arcpy.ExportRasterCatalogPaths_management(fullname, "ALL", pathtable)
    except Exception as e:
        arcpy.AddMessage("Error export catalog")
        arcpy.AddMessage(arcpy.GetMessages())

def getMosaicRow(objectId, table):
    where = 'SourceOID = ' + objectId # this is my objectid field name
    #this was line that explodes
    cursor = arcpy.SearchCursor(table, where)
    fs = None
    for row in cursor:
        # fs = row # this will be a geoprocessing row object of no value outside the cursor
        sPath = row.path
        nSourceOID = row.SourceOID
        nOID = row.OID
    del cursor 
    return (nOID,nSourceOID,sPath)# return a tuple which contains the field contents

if __name__ == '__main__':
    main()

c:\workspace/scratch.gdb/Paths
Input Mosaic Dataset: D:\data\aerial\catalog\nzmgcat.gdb/nztm1
Search SourceObjectID: 40
pathtable deleted successfully
(40, 40, u'D:\\data\\aerial\\catalog\\test\\r12a.jpg')


I cannot reproduce your error in Desktop.
0 Kudos
JoeHershman
MVP Alum
Kim,

I appreciate the time you took setting t on my his up.  I kind of jump between trying to run on the server and working desktop.  I think I may try to reinstall python on my desktop and see what that does.  I basically cannot seem to create a SearchCursor in any way, even just in the py window with an existing table.


I have a related question.  I think there is something I am just not grasping here.  I do have the script successfully creating the path output table as a geoprocessing service.  I did finally realize that my output variable should be index 3, as you note in your comments (was thinking output started at 0)

So I have the table in my scratch workspace and I want to to just send that table back to the caller of the service as a result how does one go about doing that.  I have tried different ways of defining the output parameter and calling both SetParameter or SetParamaterAsText.  Always get that "Object error in setting parameter....'.  I know there is something really fundamental I am just not getting my head around, could you possibly point me in the right direction?

Cheers
-Joe
Thanks,
-Joe
0 Kudos
KimOllivier
Honored Contributor
Sorry, can't help with server. I might now try installing ArcServer 10.1 since it is easier to manage.
0 Kudos