Arcpy scripting works in Python window but not in Script

7177
12
08-04-2011 12:00 PM
LorindaGilbert
Occasional Contributor II
Okay, I am really at my wits end now.  The three previous posts were with the script - all written after testing in the python window.  Now I see that I am not having difficulties with the language even if I am just learning it, for these simple tasks, I have gotten the process.

Can SOMEONE please explain why the individual lines of script work in the python window from within an ArcMap session, but DO NOT work when saved as a script to be used as a tool?

The entire script is really quite simple:
After the mxd is opened in ArcMap,
Geocode some data (already know that a composite locator won't work on a Win7 x64 in script)
Update the symbology
Save the geocoded results as a fc in fgd with name_userid_date
Append the geocoded results to an existing fc in fgd with name_year_userid
Then the user prints maps as required

I mean really, this SHOULD NOT be this difficult.  When I tried the geocode portion, discovered that the composite wouldn't run in the script.  Then the update symbology works in window, not in script.  Save the geocoded results to either the current geocoded with date or append to the yearly file, get either an error 582 or the warning 117 of empty set created.

I have tried this on a Win7 x64 Arc10 SP1 machine and on an XP x32 SP3 Arc10 SP2 machine - scripting doesn't work on either one.

Guess the good thing is that we can always teach the new folks how to do the process the old fashioned way and then when we can figure out WHY the script isn't working and get it working then we can give them the tool.
0 Kudos
12 Replies
DanPatterson_Retired
MVP Emeritus
You are lacking specific details.  I write scripts all the time that work within Pythonwin or Pyscripter and/or as tools within Arctoolbox.
1  What are your installation details?  Was it default installation on a local machine or in some networked environment?
2  Did you change the version of Python that was installed and not use the one that comes with ArcGIS?
3  Machine specifics will not be relevant, but the operating environment will be as will samples of simple code that works in one environment and not in another.
4  Provide error messages as well as copies of the failing scripts.

As of version 10, ArcGIS is pretty tightly tied to Python and if you want, the version of Pythonwin installed from their installation CD's.  Problems seem to arise when it isn't a local install...people want to use a different version of Python or a different Python IDE.  Keep it simple or narrow down where your environment is different from the rest of the installations that have no problems.
Good luck
0 Kudos
DaleHoneycutt
Occasional Contributor III
Are you making a script tool via the script tool wizard, or running it from the operating system prompt?

Please post the script tool code...
0 Kudos
LorindaGilbert
Occasional Contributor II
Well, the script parts were posted in the other posts that no one gave any insight into, didn't want to clutter yet another post with the same info, but I will.
This is a default, clean install on new local machines.  Everything from the download (yes, burnt to a dvd).  These scripts don't work on two different machines, one a clean install and the other an upgrade from 9.3.1 - so how does that fit into the picture?  And yes, I use pythonwin that came from the install disk with 2.6 Python on the local machine.
Then I am using the script tool wizard to create the tool that I eventually want to add as a button on the toolbar.

Copied from previous posts:

Part of the script that I am writing is to take the geocoded results and copy them both to an existing yearly file and to a file that is stamped with the date run and the userid. Both of these will 'create' in the file geodatabase but both return the WARNING 000117: Warning empty output generated.
I have checked the spatial reference for both the copied from the original feature class and the newly created feature class and both are the SAME as the geocoded results file. I would expect to get this warning on the one that I am running fc2fc using an empty template for, but not for the one that I am appending data to a direct copy of the original. What else causes this VERY helpful warning? This was also tried with the CopyFeatures_management command as well - same results.The if..else section criteria about the arcpy.exists will be changed in the future, I just want it to work right now.

Python code snippet: (copy and paste removed the indents)

# import
import arcpy, time, datetime, getpass

#getting current time and user
now = datetime.datetime.today()
date_stamp = now.strftime("%m%d%Y")
maintid = getpass.getuser()
curyear = now.strftime("%Y")

#variables
wksp = arcpy.env.workspace = (r"j:\NewData.gdb")
mxd = arcpy.mapping.MapDocument(r"j:\mxdfiles\NewData.mxd")
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
saveGeocode = arcpy.GetParameterAsText(0)
outFile = r"\FeatTable_"+maintid+"_"+date_stamp
yearlyFile = wksp+"\FeatTable"+curyear+"_"+maintid
curGeocode = wksp+outFile
templateFC = wksp+"\FCTemplate" #this is a feat class with only the structure

#Set up the env
#arcpy.env.overwriteOutput = True

if saveGeocode == 'Y' or saveGeocode == 'y':
layer2manipulate = arcpy.mapping.ListLayers(mxd,"FeatTable",df)[0]
if arcpy.Exists(layer2manipulate):
arcpy.FeatureClassToFeatureClass_conversion(templateFC,wksp,outFile)
arcpy.Append_management(layer2manipulate,yearlyFile,"NO_TEST")
arcpy.Append_management(layer2manipulate,curGeocode,"NO_TEST")
arcpy.AddMessage(r"Geocoded data has been archived (saved) for future reference.")
else:
arcpy.AddMessage(r"You must run the geocoding process first.")
else:
arcpy.AddMessage(r"Geocoding data will not be saved and will be overwritten next time.")


When attempting the composite locator - that someone else let me know doesn't work with Win7 x64, so we have worked around that by using just the street for now.
snippets of code:
CAUfile = "caufile.txt"
AddLoc1 = "PointAddressLocator"
AddLoc2 = "StreetLocator"
AddComp = "CompositeLocator"
GeoColumn = "Address Adress VISIBLE NONE"

arcpy.env.overwriteOutput = True

arcpy.GeocodeAddresses_geocoding(CAUfile,AddComp,GeoColumn,wksp+"\Geoed")

I have tried it as well with GeoColumn = "Address"

Running Win7 x64; Arc10 SP1

The PointLocator uses single field of address
StreetLocator uses Address with nothing for the City

When attempting to update the geocoded results:
mxd = arcpy.mapping.MapDocument(r"j:\mxdfiles\mxdname.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

updateLayer = arcpy.mapping.ListLayers(mxd, "TestFC", df)[0]
sourceLayer = arcpy.mapping.Layer(r"j:\test\test.lyr")

arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer)
arcpy.RefreshTOC()
arcpy.RefreshActiveView()


Nothing happens in the arcmap window.

Yet, most of all of these lines of code when copied and pasted in the python window in the Arcmap session work.  What gives?
0 Kudos
JasonPardy
New Contributor
I believe all you are missing is to do a save of the mxd. In the Python Window, you see it work after the Refreshes because you have the mxd open. However, running this code outside ArcGIS will required a save of the mxd or save a copy of the mxd.

mxd.save()

or

mxd.saveACopy(r"C:\Project\Project2.mxd")
0 Kudos
LorindaGilbert
Occasional Contributor II
But what happens if you DO NOT want to save the mxd?
0 Kudos
JasonPardy
New Contributor
If you are running this script outside the ArcGIS process space, you will need to save the changes or save a copy. You cannot run the script in a Python IDE such as Pythonwin and refresh the current map running in ArcMap--they are separate processes and do not communicate with each other. Your only option if you don't want to save or save a copy is to load your script into the Python window and execute.

Hope this helps.

If you decide to save or save a copy, please post your results so we know it resolved your issue.

Many thanks,
Jason
0 Kudos
LorindaGilbert
Occasional Contributor II
Okay, the goal is to have the user open the mxd and have the script run as a tool on the toolbar inside of arcmap.  After the geocoding process and saving the geocoded results they will be printing maps.  Can't automate that portion cause they need to move around in the mapped area.  So technically, it is all to be run within an ArcMap session.

The script doesn't work except when I copy the individual lines of code into the python window in arcmap.  I am using the script wizard tool to create the 'tool' in a toolbox and have added it as a geoprocessing tool button in the toolbar.
0 Kudos
JasonPardy
New Contributor
Oh ok. Try this:

1. Ensure the 'run in process option' in the Script tool properties is checked on.
2. Rather than hard code the path to the mxd, use the current mxd
mxd = arcpy.mapping.MapDocument('current')

Jason
0 Kudos
LorindaGilbert
Occasional Contributor II
Yes, 'run in process' is selected.
Tried the "Current" and it didn't seem to make a difference.
Jazmateta
0 Kudos