< BACKMake Note | BookmarkCONTINUE >
156135250194107072078175030179198180025031194137176049106218111004228145120028211154087132

Executing Other (Python) Programs

When we discuss the execution of other programs, we distinguish between Python programs and all other non-Python programs, which includes binary executables or other scripting language source code. We will cover how to run other Python programs first, then how to use the os module to invoke external programs.

Import

During run-time, there are a number of ways to execute another Python script. As we discussed in an earlier chapter, importing a module the first time will cause the code at the top-level of that module to execute. This is the behavior of Python importing, whether desired or not. We remind you that the only code that belongs to the top-level of a module are global variables, and class and function declarations.

These should be followed by an if statement that checks __name__ to determine if a script is invoked, i.e., "if __name__ == '__main__'". In these cases, your script can then execute the main body of code, or, if this script was meant to be imported, it can run a test suite for the code in this module.

One complication arises when the imported module itself contains import statements. If the modules in these import statements have not been loaded yet, they will be loaded and their top-level code executed, resulting in recursive import behavior. We present a simple example below. We have two modules import1 and import2, both with print statements at their outermost level. import1 imports import2 so that when we import import1 from within Python, it imports and "executes" import2 as well:

Here are the contents of import1.py:

						
# import1.py
print 'loaded import1'
import import2

					

And here are the contents of import2.py:

						
# import2.py
print 'loaded import2'
					

Here is the output when we import import1 from Python:

						
>>> import import1
loaded import1
loaded import2
>>>
					

Following our suggested workaround of checking the value of __name__, we can change the code in import1.py and import2.py so that this behavior does not occur:

Here is the modified version of import1.py:

						
# import1.py
import import2
if __name__ == '__main__':
      print 'loaded import1'

					

The following is the code for import2.py, changed in the same manner:

						
# import2.py
if __name__ == '__main__'
      print 'loaded import2'

					

We no longer get any output when we import import1 from Python:

						
>>> import import1
>>>

					

Now it does not necessarily mean that this is the behavior you should code for all situations. There may be cases where you want to display output to confirm a module import. It all depends on your situation. Our goal is to provide pragmatic programming examples to prevent unintended side effects.

execfile()

It should seem apparent that importing a module is not the preferred method of executing a Python script from within another Python script; that is not what the importing process is. One side effect of importing a module is the execution of the top-level code.

Earlier in this chapter, we described how the exec statement can be used with a file object argument to read the contents of a Python script and execute it. This can be accomplished with the following code segment:

						
f = open(filename, 'r')
exec f
f.close()

					

The three lines can be replaced by a single call to execfile():

						
execfile(filename)
					

Although the code above does execute a module, it does so only in its current execution environment (i.e., its global and local namespace). There may be a desire to execute a module with a different set of global and local dictionaries instead of the default ones. For this purpose, we can use the execfile() built-in function, whose syntax allows the programmer to specify the namespaces:

						
execfile(filename, globals=globals(), locals=locals())
					


Last updated on 9/14/2001
Core Python Programming, © 2002 Prentice Hall PTR

< BACKMake Note | BookmarkCONTINUE >

© 2002, O'Reilly & Associates, Inc.