''' 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)
''' 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
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)
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)
Fraxinus: you can also import a toolbox and run a script (in the toolbox) that way.
import script2.py
import sys
In case anyone is looking for that Python template webpage, it can be found here: