Running Python scripts (with parameters) within another Python script with ArcPy?

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.


You can accomplish this by importing a module (ie script) into your main script and calling its functions. A simple demo is contained in the accompanying two scripts.

    '''
Main_program.py

demonstrates how to import and call functions from another module
'''
import sys
import CallingFunctions

a_list = [1,2,3,4,5,6,7,8,9,10]
print sys.argv[0]
print CallingFunctions.func1(a_list)
print CallingFunctions.func5(a_list)
print CallingFunctions.func8(a_list)

for the main program and for the functions that are getting called

'''
Callingfunctions.py

imported into another program giving it access to the functions
'''

def func1(inputs=None):
  x = sum(inputs)
  return "sum some numbers: ", x
'''
more functions
'''
def func5(inputs=None):
  x_sq = 0
  for x in inputs:
    x_sq += x**2
  return "sum of squares: ", x_sq
'''
more functions
'''
def func8(inputs=None):
  return "hello from 8: ", inputs

'''
more functions
'''
if __name__ == "__main__":
  a_list = [1,2,3,4,5,6,7,8,9,10]
  inputs = "test inputs"
  a_dict = {1:[func1([1,2,3]) ],
            5:[func5([1,2,3])],
            8:[func8("inputs to 8")]}
  needed = [1,5,8]
  for akey in needed:
    if akey in a_list:
      action = a_dict[akey]
      print "\naction: ", action

you just have to ensure that the main module and the child module are in the same folder. You can pass parameters to the child module easily and if the child module needs access to arcpy (assuming you are using version 10 of arcmap) simply pass a reference to it.


Importing and running a function is the cleaner way to do it, but for the sake of completeness, there is also the execfile built-in function (documentation) which will let you run an arbitrary file in the current context.