+ Reply to Thread
Results 1 to 9 of 9

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

  1. #1
    Aaron Paul
    Join Date
    Nov 2010
    Posts
    22
    Points
    0
    Answers Provided
    0


    0

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

    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???

  2. #2
    Dan Patterson

    Join Date
    Apr 2010
    Posts
    1,756
    Points
    442
    Answers Provided
    41


    0

    Default Re: How do you call a Script from a Script????

    Here is a sample program that imports a user module which contains functions

    Code:
    '''
    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
    Code:
    '''
    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
    Geomatics, Carleton University, Ottawa, Canada
    http://obidangis.blogspot.ca/

  3. #3
    Chris Snyder

    Join Date
    May 2010
    Posts
    1,262
    Points
    417
    Answers Provided
    38


    0

    Default Re: How do you call a Script from a Script????

    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)

  4. #4
    Dan Patterson

    Join Date
    Apr 2010
    Posts
    1,756
    Points
    442
    Answers Provided
    41


    0

    Default Re: How do you call a Script from a Script????

    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
    Geomatics, Carleton University, Ottawa, Canada
    http://obidangis.blogspot.ca/

  5. #5
    Jamie Kass
    Join Date
    Apr 2010
    Posts
    96
    Points
    6
    Answers Provided
    1


    0

    Default Re: How do you call a Script from a Script????

    Quote Originally Posted by csny490 View Post
    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.

  6. #6
    Hugo Ahlenius
    Join Date
    May 2010
    Posts
    132
    Points
    10
    Answers Provided
    3


    0

    Default Re: How do you call a Script from a Script????

    you can also import a toolbox and run a script (in the toolbox) that way.
    --------------------
    Hugo Ahlenius
    Nordpil - GIS consultants

  7. #7
    Chris Bater
    Join Date
    Nov 2010
    Posts
    48
    Points
    4
    Answers Provided
    0


    0

    Default Re: How do you call a Script from a Script????

    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)
    Chris Bater
    Alberta SRD

  8. #8
    Curtis Price

    Join Date
    Oct 2009
    Posts
    1,798
    Points
    874
    Answers Provided
    127


    0

    Default Re: How do you call a Script from a Script????

    Quote Originally Posted by passiveoptical View Post
    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
    Code:
    import script2.py
    will find it.

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

    [code]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.
    Last edited by curtvprice; 05-30-2012 at 02:39 PM.

  9. #9
    Jason Scheirer

    Join Date
    Oct 2009
    Posts
    514
    Points
    491
    Answers Provided
    69


    0

    Default Re: How do you call a Script from a Script????

    Go the import route or use the execfile function.

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts