How do you call a Script from a Script????

1835
11
03-30-2011 10:31 AM
AaronPaul
New Contributor II
I'm creating what I call a "Script Manager" that will fire a script based on product type.
So, if the user inputs Aerial or Radius, the appropriate script is ran.

I can get this to run using os.system('python C:\Sample.py' + parameter variables)
The problem with this is the script is run through python window.

I've been told it is more effective to use an import statement, something like this:
if Product == "Aerial":
      import ("C:\Sample.py")

What am I missing???
Tags (2)
0 Kudos
11 Replies
DanPatterson_Retired
MVP Emeritus
Here is a sample program that imports a user module which contains functions

'''
Main_program.py

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

a_list = [1,2,3,4,5,6,7,8,9,10]

print CallingFunctions.func1(a_list)
print CallingFunctions.func5(a_list)
print CallingFunctions.func8(a_list)


The module that is imported is here
'''
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


if the two *.py files are located within the same directory, you can import the CallingFunctions module and use its functions from the Main_program.  In this manner you can create "helper" modules that can be used by other programs.  you should also note, that for testing purposes, the CallingFunctions can be run in standalone mode with the addition of the code block denoted by the "if" statement towards the end
ChrisSnyder
Regular Contributor III
The Python import statement looks for the specified "module" (aka a .py/.pyc file) in all the directories that are listed in sys.path(). For me, these include: ['C:\\Program Files\\ArcGIS\\bin', 'C:\\WINDOWS\\system32\\python25.zip', 'C:\\Python25\\DLLs', 'C:\\Python25\\lib', 'C:\\Python25\\lib\\plat-win', 'C:\\Python25\\lib\\lib-tk', 'C:\\Python25\\Lib\\site-packages\\pythonwin', 'C:\\Python25', 'C:\\Python25\\lib\\site-packages', 'C:\\Python25\\lib\\site-packages\\win32', 'C:\\Python25\\lib\\site-packages\\win32\\lib']

It easiest just to put your module in one of these default paths. However, if you make your own module, and say want to put it out on a network location somewhere:

import sys
networkPath = r"\\mynetwork\gis\py_files" #aka the folder where the mymodule.py file is located
sys.append(networkPath)
import mymodule
mymodule.myFunction(arg1,arg2)
DanPatterson_Retired
MVP Emeritus
and to add to that path list, it looks in the path where the main script resides, which makes it useful and easier to distribute scripts and toolboxes as one grouping in one folder
0 Kudos
JamieKass
Occasional Contributor

It easiest just to put your module in one of these default paths. However, if you make your own module, and say want to put it out on a network location somewhere:

import sys
networkPath = r"\\mynetwork\gis\py_files" #aka the folder where the mymodule.py file is located
sys.append(networkPath)
import mymodule
mymodule.myFunction(arg1,arg2)


Just a small correction: the correct syntax is sys.path.append(networkPath). Thank you for this thread, guys.
HugoAhlenius
Occasional Contributor
you can also import a toolbox and run a script (in the toolbox) that way.
0 Kudos
ChrisBater
New Contributor II
When I import a module I've written, I also like to reload the module to update any changes I may have made to it, so:

sys.path.append(r'modulePath')
import module
reload(module)
0 Kudos
curtvprice
MVP Esteemed Contributor
When I import a module I've written, I also like to reload the module to update any changes I may have made to it, so:

sys.path.append(r'modulePath')
import module
reload(module)



The reload() is only necessary if you are in an interactive python session. An import will always read your (updated) source at compilation time, unless the module was already imported "upstream", ie by code that is calling your program.

Fraxinus: you can also import a toolbox and run a script (in the toolbox) that way.


Calling a script tool from a script works, but can cause problems with performance and stability. A better way to call another script is to set it up so you can call the script either as a script tool or as a Python function. This is done using the __name__ property, which is always '__main__' when you are running the script directly, say, as a script tool, double clicking the .py file, or entering the name of the .py file at a system prompt.

There's a nice example of this in this Esri blog post by Dale Honeycutt:


'>ArcGIS Blog: PythonTemplate


To reiterate what Dan pointed out, the folder "." (relative to the script) is always in the sys.path, so if you if the other script is in the same folder as this one
import script2.py

will find it.

You can place a folder scripts at paths relative to the your script and locate them this way:

import sys

import os
Here = os.path.dirname(sys.argv[0])
sys.path.append(Here + "/utils")
import script2 # will find here/Utils/script2.py

You can also set up a "package" to look through a whole folder tree, but that's generally beyond the scope of a simple ArcGIS script tool.
0 Kudos
AndresCastillo
MVP Regular Contributor

In case anyone is looking for that Python template webpage, it can be found here:

https://web.archive.org/web/20151217180732/http://blogs.esri.com:80/esri/arcgis/2011/08/04/pythontem...

0 Kudos
JasonScheirer
Occasional Contributor III
Go the import route or use the execfile function.