Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a rather long script that first references an SDE feature class (non-versioned) to perform an attribute selection based on a SQL selection. I've been unable to figure out why the script will run once fine, but if I run it again I always get an error at the point of the SelectLayerByAttribute_management step.

My environment: ArcGIS 10.1 SP1, Win7 SP1, and the source file is on an Oracle(11g) SDE server accessed through database authentication.

Error message: "ARCPY ERROR 000582: Error occurred during execution."

This is the code I start with:

arcpy.env.workspace = "IN_MEMORY"
mxd = arcpy.mapping.MapDocument("CURRENT")
mm = arcpy.mapping.ListDataFrames(mxd, "MainMap")[0]
qsLayer = arcpy.mapping.ListLayers(mxd, "EAS QS Grid", mm)[0]

qs = "10-28"
qry = "TILE = '%s'" % (qs)
arcpy.SelectLayerByAttribute_management(qsLayer, "NEW_SELECTION", qry)

When I look at the GP Results window, I see that the Output Layer Name is GPL0, not "EAS QS Grid". I've tried using the Select by Attribute tool from the toolbox and after running the results show the correct layer name. So what is happening when using arcpy to have the layer name changed to GPL0? This script also performs similar selections to other layers in the MXD, five SelectLayerByAttribute and two SelectLayerByLocation. All the Output names always show as GPL0. This seems like it could cause Arc to trip over itself if GPL0 is somehow locked in memory after the script runs, but I can't find any documentation about the behavior of Geoprocessing Layers(GPL?) or why a Select Layer By Attribute tool would even need to make a new Output Layer Name.

Things I've tried: - 'del qsLayer' at the end of the script - Not using IN_MEMORY workspace - Deleting the IN_MEMORY workspace at the end of the script - Setting the overwriteOutput to True at the top of the script - Using qsLayer.dataSource as the parameter for SelectByAttribute - Yes, GP Background Processing is not turned on

So far none of that seems to change the outcome.

The error message does not provide any useful feedback, so I want to ask the web if anyone has had problems with this kind of issue. Right now the only way I can use the script is to run it once, close ArcMap and reopen, then it's good to go one more time.

Thank you for any suggestions.


In my excitement at getting the script working I rushed to post my answer, and of course it failed anyway. Using arcpy.mapping.Layer was not the solution...

What I did figure out after picking apart the script and running over and over until I had no errors is that I'm creating a conflict with the data frame layers. My script is intended to shuffle 14 different data frames around a "Main Map" data frame layout for printing, and each data frame has an identical set of layers, the only difference is the scale and extent of each frame. What I'm trying to achieve is not having all the layers turned on at the start, so I used the code below:

allLayers = arcpy.mapping.ListLayers(mxd)
for lyr in allLayers:
    if lyr.visible == True:
        lyr.visible = False
del lyr, allLayers

I do that step again at the end of the script to turn all the layers on by switch the True and False statement. That will run fine once, but run it twice and it fails.

Now that I've narrowed down my issue to this, how can I turn on and off the layers in 14 different data frames without causing conflict with the way I've referenced my other layers in the MainMap data frame?

share|improve this question

To try and reproduce your problem I modified your script to use my own map, data frame, layer, field and value as below - I also got it to print out the layer name:

arcpy.env.workspace = "IN_MEMORY"
mxd = arcpy.mapping.MapDocument("CURRENT")
mm = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
qsLayer = arcpy.mapping.ListLayers(mxd, "Countries", mm)[0]


qs = "Australia"
qry = "sovereignt = '%s'" % (qs)
arcpy.SelectLayerByAttribute_management(qsLayer, "NEW_SELECTION", qry)

I was able to copy/paste this code into the Python window and repeatedly get the expected result. If this does not work for you perhaps we can look more closely at what is different between my test and yours. I am using ArcGIS 10.1 SP1 on Windows 7 SP1 and the source of the layer is shapefile or file geodatabase (I tested both).

As an aside, I don't think the line below is important to the code snippet we are testing.

arcpy.env.workspace = "IN_MEMORY"
share|improve this answer
I edited my question to include my work environment. It's a wild guess, but the GP tool might not be handling the path to the SDE file correctly. – user18412 Jun 21 '13 at 23:32
I will be very surprised if it is that you are using ArcSDE when I am in this test using shapefile/File Geodatabase. I use code like yours against ArcSDE layers on an almost daily basis. The only difference is your where clause construction which is why I tested that to make sure it works. – PolyGeo Jun 21 '13 at 23:37
PolyGeo, I deleted my last comment after realizing my first fix was a no go. I've edited my question. But still, thanks for looking into my issue. – user18412 Jun 24 '13 at 22:57
I'm finding it even harder to follow what you have done and are trying to do now that you have updated your original question with details of a second one. I would recommend that you resolve the initial one here and split the new issue off into a new Question. – PolyGeo Jun 25 '13 at 7:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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