Can't get the workspace set correctly,  or it's something else.

1219
7
Jump to solution
05-02-2014 02:49 PM
PaulHuffman
Occasional Contributor III
Last week I was rearranging the attribute tables of a bunch of shapefiles.  It was working fine. It looked like this:
# Import arcpy module import arcpy from arcpy import env env.workspace = "C:\\avdata\\FallChinookRedds\\2013\\Shapefiles\\"  # Local variables: inshape = "MDhighway972211113.shp" fname = "\""+inshape+"\"" surdate = "\"11/01/2013\"" streamname = "\"Yakima River\"" specname = "\"Fall Chinook\""  # Process: Delete Field arcpy.DeleteField_management(inshape, "TYPE;Y_PROJ;X_PROJ;DISPLAY;SYMBOL;UNUSED1;DIST;PROX_INDEX;COLOR;DEPTH;TEMP;TIME;WPT_CLASS;SUB_CLASS;ATTRIB;LINK;STATE;COUNTRY;CITY;ADDRESS;FACILITY;CROSSROAD;UNUSED2;ETE;DTYPE;MODEL")  #more steps ......


Most of the time I was editing python, changing the shapefile name, then dragging the code from IDLE to the ArcMap python window so I could keep the map open and not hit schema locks. 

This week I was doing the same thing to some 2012 shapefiles.  I thought all I had to do was change the workspace env variable and it would work the same:
# Import arcpy module import arcpy from arcpy import env env.workspace = "C:\\avdata\\FallChinookRedds\\"  # Local variables: inshape = "2zillTOgra102312.shp" fname = "\""+inshape+"\"" surdate = "\"10/23/2012\"" streamname = "\"Yakima River\"" specname = "\"Fall Chinook\""  # Process: Delete Field arcpy.DeleteField_management(inshape, "TYPE;Y_PROJ;X_PROJ;DISPLAY;SYMBOL;UNUSED1;DIST;PROX_INDEX;COLOR;DEPTH;TEMP;TIME;WPT_CLASS;SUB_CLASS;ATTRIB;LINK;STATE;COUNTRY;CITY;ADDRESS;FACILITY;CROSSROAD;UNUSED2;ETE;DTYPE;MODEL") #more steps .....


but in the python window I get "ERROR 000732: Input Table: Dataset 2zillTOgra102312.shp does not exist or is not supported Failed to execute (DeleteField).  " as soon as it hits that first DeleteField, as if the file name or path is wrong.  However if I print env.workspace in the python window, I get the path I expect. And if I type listshapes = arcpy.ListFeatureClasses() , listshapes is filled with all the shapefiles in the workspace. Tried several other input shapefiles, but got the same type of error.

The script for 2012 data works when I run it in python, but I have to shut down ArcMap and ArcCatalog to avoid hitting schema locks.  What am I missing when I try to run this in the python window?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
PaulHuffman
Occasional Contributor III
ESRI tech support did a screen share with me and quickly found the problem.  The "enable background processing" check box was on in the 2012 map document.  Even though I could "print env.workspace" and get the correct path,  background processing somehow resets everything, apparently, on execution of a next line so the workspace path and the input shapefile can't be found.  ESRI thought they might submit a bug report on it, since this doesn't seem to be desired or expected behavior. 

From all the posts in this forum, it seems like "enable background processing" is often involved in unexpected behavior in many different situations.

View solution in original post

0 Kudos
7 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Paul,

Do all of the shapefiles begin with a number?  When naming shapefiles/feature classes it's recommended to:

1.  Not start the name with a number
2.  Not have the name contain spaces
3.  Not have the name contain special characters
0 Kudos
PaulHuffman
Occasional Contributor III
Thanks Jake,  that's always good advice on naming shapefiles.  But I didn't name them. This is the hand I was dealt. Looks like I'm going to have to retrain these crews. I'm dealing with lots of tiny GPS files collected in the wrong projection with elaborate file names rather than attributes like date in the attribute tables.  Two weeks ago I was dealing with removing spaces and - from the file names.   http://forums.arcgis.com/threads/107870-AddLayer-and-.txt-files

However, in this case the script will run correctly from the IDLE window, but I have to shut down the ArcCatalog and ArcMap to get an exclusive schema lock.  Also, I changed the name of one shapefile to "dog.shp",  and the script gave me the same workspace error in the ArcMap python window.
0 Kudos
curtvprice
MVP Esteemed Contributor
Since you are playing the dangerous game of working parallel with two apps in the same workspace, I'm thinking you may want to try this method to have arcpy verify what's REALLY there in the workspace, just not last time it checked:

arcpy.RefreshCatalog(env.workspace)
0 Kudos
PaulHuffman
Occasional Contributor III
Today I've been successfully cranking through these shapefiles with this script. Making so much progress I started to think that yesterday I just had a typo in the workspace path that I didn't catch.  But now I see that it doesn't matter if the shapefile name is 13 characters long. It doesn't matter if the shapefile name starts with a numeral.  What causes my script to fail is running it in the ArcMap Python window.  It works fine in the ArcCatalog python window, or from IDLE if I shut down ArcMap and ArcCatalog.  In ArcMap, is there some env variable that persists even after the script sets the env.workspace variable?  Does it revert to the map properties right after the script runs the env.workspace line?

update: I looked again at the map document properties. Home folder was set correctly, the default geodatabase was set to a different location, so I made a test file geodatabase in the home directory, set it as default.  Then I examined the map document geoprocessing options, and made sure that the workspace was set to the home folder.  The home folder contains all the shapefiles and a geodatabase is not used in the map.  Yet when I try to execute any geoproccessing line in my code in the ArcMap python window, I get "ERROR 000732: Input Table: Dataset MarionHarrahTOoldgold102512.shp does not exist or is not supported Failed to execute (CalculateField)."  as if the path is wrong.  This is code that works in ArcCatalog or IDLE.  I don't know what is different about ArcMap python window.
0 Kudos
PaulHuffman
Occasional Contributor III
Since you are playing the dangerous game of working parallel with two apps in the same workspace, I'm thinking you may want to try this method to have arcpy verify what's REALLY there in the workspace, just not last time it checked:

arcpy.RefreshCatalog(env.workspace)


Didn't help.
0 Kudos
PaulHuffman
Occasional Contributor III
What makes this more weird is I was running a script much like this two weeks ago in the python window in a different map document.  I opened up the 2013 data map document again and tried it. Yes, this script still runs, finds the shapefile just fine, just a different workspace, different map, same terrible long shapefile names.  There must be something different about the 2012 map.  Can there be something wrong with the workspace? 

# Import arcpy module
import arcpy
from arcpy import env
env.workspace = "C:\\avdata\\FallChinookRedds\\2013\\Shapefiles\\"

# Local variables:
inshape = "MDhighway972211113.shp"
fname = "\""+inshape+"\""
surdate = "\"11/01/2013\""
streamname = "\"Yakima River\""
specname = "\"Fall Chinook\""

# Process: Delete Field
arcpy.DeleteField_management(inshape, "TYPE;Y_PROJ;X_PROJ;DISPLAY;SYMBOL;UNUSED1;DIST;PROX_INDEX;COLOR;DEPTH;TEMP;TIME;WPT_CLASS;SUB_CLASS;ATTRIB;LINK;STATE;COUNTRY;CITY;ADDRESS;FACILITY;CROSSROAD;UNUSED2;ETE;DTYPE;MODEL")

# Process: Add Field
#arcpy.AddField_management(inshape, "COMMENT", "TEXT", "", "", "50", "", "NON_NULLABLE", "NON_REQUIRED", "")
#arcpy.AddField_management(inshape, "ALTITUDE", "DOUBLE", "", "", "8", "", "NON_NULLABLE", "NON_REQUIRED", "")
#arcpy.AddField_management(inshape, "FILENAME", "TEXT", "", "", "254", "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(inshape, "SurveyDate", "DATE", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(inshape, "SurveyYear", "LONG", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(inshape, "observer", "TEXT", "", "", "25", "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(inshape, "PassNum", "SHORT", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(inshape, "Species", "TEXT", "", "", "50", "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(inshape, "Stream", "TEXT", "", "", "50", "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(inshape, "ReddStatus", "TEXT", "", "", "3", "", "NON_NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field
arcpy.CalculateField_management(inshape, "SurveyDate", surdate, "VB", "")
arcpy.CalculateField_management(inshape, "SurveyYear", "2013", "VB", "")
arcpy.CalculateField_management(inshape, "Stream", streamname, "VB", "")
#arcpy.CalculateField_management(inshape, "observer", "\"Brandon Rogers\"", "VB", "")
#arcpy.CalculateField_management(inshape, "PassNum", "PASS", "VB", "")
arcpy.CalculateField_management(inshape, "Species", specname, "VB", "")
arcpy.CalculateField_management(inshape, "FILENAME", fname, "VB", "")

arcpy.GetCount_management(inshape)

0 Kudos
PaulHuffman
Occasional Contributor III
ESRI tech support did a screen share with me and quickly found the problem.  The "enable background processing" check box was on in the 2012 map document.  Even though I could "print env.workspace" and get the correct path,  background processing somehow resets everything, apparently, on execution of a next line so the workspace path and the input shapefile can't be found.  ESRI thought they might submit a bug report on it, since this doesn't seem to be desired or expected behavior. 

From all the posts in this forum, it seems like "enable background processing" is often involved in unexpected behavior in many different situations.
0 Kudos