15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

14.4. Executing Other (<strong>Python</strong>) Programs<br />

When we discuss the execution of other programs, we distinguish between <strong>Python</strong> programs and all<br />

other non-<strong>Python</strong> programs, which include binary executables or other scripting language source code.<br />

We will cover how to run other <strong>Python</strong> programs first, then how to use the os module to invoke external<br />

programs.<br />

14.4.1. Import<br />

During runtime, there are a number of ways to execute another <strong>Python</strong> script. As we discussed earlier,<br />

importing a module the first time will cause the code at the top level of that module to execute. This is<br />

the behavior of <strong>Python</strong> importing, whether desired or not. We remind you that the only code that<br />

belongs to the top level of a module are global variables, and class and function declarations.<br />

<strong>Core</strong> Note: All modules executed when imported<br />

This is just a friendly reminder: As already alluded to earlier in<br />

Chapters 3 and 12, we will tell you one more time that <strong>Python</strong><br />

modules are executed when they are imported! When you import the<br />

foo module, it runs all of the top-level (not indented) <strong>Python</strong> code, i.<br />

e., "main()". If foo contains a declaration for the bar function, then def<br />

foo(...) is executed. Why is that again?<br />

Well, just think what needs to be done in order for the call foo.bar()<br />

to succeed. Somehow bar has to be recognized as a valid name in the<br />

foo module (and in foo 's namespace), and second, the interpreter<br />

needs to know it is a declared function, just like any other function in<br />

your local module.<br />

Now that we know what we need to do, what do we do with code that<br />

we do not want executed every time our module is imported? Indent it<br />

and put it in the suite for the if __name__ == '__main__'.<br />

These should be followed by an if statement that checks __name__ to determine if a script is invoked, i.<br />

e., "if__name__ == '__main__'". In these cases, your script can then execute the main body of code, or,<br />

if this script was meant to be imported, it can run a test suite for the code in this module.<br />

One complication arises when the imported module itself contains import statements. If the modules in<br />

these import statements have not been loaded yet, they will be loaded and their top-level code<br />

executed, resulting in recursive import behavior. We present a simple example below. We have two<br />

modules import1 and import2, both with print statements at their outermost level. import1 imports<br />

import2 so that when we import import1 from within <strong>Python</strong>, it imports and "executes" import2 as well.<br />

Here are the contents of import1.py:<br />

# import1.py<br />

print 'loaded import1'

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!