| [ Web Proxy ] |
| Viewing: https://wiki.python.org/moin/../python/platforms/MacPython/../../../python/archive/GIS/../JimD.html | [Back] [Original] |
Legacy Wiki Page
This page was migrated from the old MoinMoin-based wiki. Information may be outdated or no longer applicable. For current documentation, see python.org.
A colleague has recently been thrust into Python programming because we needed to create our own tools around some of the Opsware tools and using their APIs. He recently posed a question: Is it a sin to not to include if__name__=='__main__': in a Python file?
As we become accustomed to Python its easy for us to take the if__name__ convention for granted. My personal opinion is that it belongs in every .py file even if only to serve as a point of documentation (put a pass statement or print an error message if the file represents code which has no meaningful standalone purpose). However I still wouldnt call it a sin to omit it.
In my response I came up with the following guidelines for him:
if__name__=='__main__': should be in every Python file with the following possible exeptions:
A script NOT ending in .py (cant be accidentally imported so its not strictly needed)
A module which is NOT marked executable (cant be accidentally executed as a script)
Notably, the construct is harmless in both these cases as well. Properly used its inclusion will prevent the two problems that might arise from changes to filenames or permissions.
What if you execute something which is purely a module? Nothing happens. (All the definition and initialization code is processed and then the interpreter drops off the end of the file). Appending a warning like: Nothing to do! This file should be imported. is harmlessly helpful in case someone makes the file executable and someone (possibly someone else) blindly executes it.
What if you import a script without an if__name__ line in it? A bunch of unintended activity possibly terminating importing script (if sys.exit() was called, for example). This is the one really bad case and one which should serve as an object lesson to any Pythonista who imports code without understanding what its supposed to be doing). Notably, the code has to be in a file with a .py extension and it has to be on the PYTHONPATH (sys.path) for this failure mode to occur.
Prepending the whole script with the if__name__ test (and indenting the whole body of code) then renders that error harmless. It runs just fine, and importing it does nothing.
In fact one could even add: if__name__!='__main__': to print an error and call sys.exit(1) if an attempt is made to import a script thats written purely as an executable. Thats perfectly legal. Theres nothing magical about if__name__== it works like any other Python conditional construct.
As I was working with Joe (my colleague) I pointed out that its a bit dubious to write code which is purely executable with no re-usability. Ive only seen that for very short, one or two line, wrappers which import and call into other Python code. I also pointed out that one can use the if__name__ suite to implement unit tests in files which are (otherwise) purely modules.
The key point here is that the purpose of the if__name__ check is to separate reusable code (definitions of classes and functions, and initializations of variables) from actions (which use the re-usable functionality). Pythons syntax encourages us to make parts of every script re-usable and it can encourage us to include unit tests localized to the same files containing code and class definitions.
Email: <aswrguyATSPAMFREEgmailDOTcom>
2026, Python Software Foundation
Made with Sphinx and Shibuya theme.
| Web Proxy Viewer | New URL | Original Page |