A common coding pattern used in AML was to run an AML (with parameters) inside another AML.
An application that I'm currently developing would benefit from being able to run a Python script (with parameters) within another Python script.
However, this does not seem to be at all straightforward.
Using ArcGIS 10, I'm experimenting with wrapping the "inner" Python script into an ArcGIS tool that has the parameters. I thought it would be a simple matter to have the "outer" Python script use arcpy.ImportToolbox to import the toolbox and then run the tool(s) within it. However, in testing so far all my attempts to run the "inner" tool from the "outer" script appear to simply skip the "inner" tool (no error is thrown).
Here is some test code to try and illustrate better what I am trying to describe.
My testinner.py script is:
inputString = arcpy.GetParameterAsText(0)
newFC = "C:\\Temp\\test.gdb\\" + inputString
arcpy.Copy_management("C:\\Temp\\test.gdb\\test",newFC)
My testouter.py script is:
import arcpy
inputString1 = arcpy.GetParameterAsText(0)
inputString2 = arcpy.GetParameterAsText(1)
arcpy.ImportToolbox("C:\\Temp\\test.tbx")
arcpy.testinner_test(inputString1)
arcpy.testinner_test(inputString2)
For testinner.py its tool needs a single String parameter.
For testouter.py its tool needs two String parameters
The two tools are placed in a test.tbx.
The test.gdb just needs a single empty feature class called test.
Once you have the above assembled, running the testinner tool with a string like 'abc' passed in as its parameter should result in feature class 'test' being copied to one called 'abc' OK.
But when you try running the testouter tool with two strings like 'uvw' and 'xyz' as its parameters, the testinner tool within testouter.py seems to run OK once, but sends ArcMap 10 SP2 on Vista SP2 to a Serious Application Error when trying to use it the second time.
The same test using Windows XP SP3 and ArcGIS Desktop 10 SP2 also produces a Serious Application Error at the same point.
Answer
Here is your test example modified to import a "utility" module within the main script and call a function using the parameters read in by the script tool:
CopyFeaturesTool.py - Script tool that reads in parameters and calls a function in another module
import CopyFeaturesUtility
import arcpy
inputFC = arcpy.GetParameterAsText(0)
outputFCName = arcpy.GetParameterAsText(1)
CopyFeaturesUtility.copyFeaturesToTempGDB(inputFC, outputFCName)
CopyFeaturesUtility.py - Module that has a single function copyFeaturesToTempGDB
. Can either be imported or run standalone. If run standalone the code under if __name__ == '__main__'
is run.
import arcpy
import os
def copyFeaturesToTempGDB(inputFeatures, outputName):
"""Copies the input features to a temporary file geodatabase.
inputFeatures: The input feature class or layer.
outputName: The name to give the output feature class."""
tempGDB = r"c:\temp\test.gdb"
newFC = os.path.join(tempGDB, outputName)
arcpy.env.overwriteOutput = True
arcpy.CopyFeatures_management(inputFeatures, newFC)
if __name__ == '__main__':
inputFC = r"c:\temp\test.gdb\test"
outputFCName = "testCopy"
copyFeaturesToTempGDB(inputFC, outputFCName)
I think you'll find this modular approach to be much more efficient and logical once you've gotten used to it. The Modules section in the standard Python tutorial is also a good resource for understanding how importing works.
For more arcpy-specific examples take a look at the built-in scripts in your C:\Program Files\ArcGIS\Desktop10.0\ArcToolbox\Scripts
folder.
No comments:
Post a Comment