45

I have a lot of simple scripts that calculate some stuff or so. They consist of just a single module.

Should I write main methods for them and call them with the if __name__ construct, or just dump it all right in there?

What are the advantages of either method?

5 Answers 5

53

I always write a main() function (appropriately named), and put nothing but command-line parsing and a call to main() in the if __name__ == '__main__' block. That's because no matter how silly, trivial, or single-purpose I originally expect that script to be, I always end up wanting to call it from another module at some later date.

Either I take the time to make it an importable module today, or spend extra time to refactor it months later when I want to reuse it for something else.

Always.

Every time.

I've stopped fighting it and started writing my code with that expectation from the start.

6
  • I normally copy paste from if name ... every time I decide to actually use a module/codeblock I made. At the very least I put them in functions dev1(), dev2()... as I am developing so that I can see what I have done. There is NEVER a reason NOT to keep code in your module (until you release it I guess) Apr 4, 2011 at 23:17
  • +1 - This is the same practice I use, a module-level main() function called by the if __name__ barrier. It is a good pattern for the creation of console_script entry points as well, e.g. 'console_scripts': ['myscript=foo.mybar:main']
    – samplebias
    Apr 5, 2011 at 0:49
  • 1
    This Scite abbreviation means I just type py<enter> in a new Python file for the full main construct. Any good editor will have a similar capabiltiy: py=#! /usr/bin/env python\n\n""""""\n\ndef main():\n\t"""Called if file executed."""\n\t|\n\nif name == 'main':\n\tmain()
    – Sean
    Mar 1, 2013 at 9:39
  • 1
    @Sean Good tip! Sublime Text has the ifmain snippet to insert the "if __name__..." part. Mar 1, 2013 at 17:47
  • @S.Lott Sorry if this is a stupid question but do you define your functions inside or outside of main()?
    – chishaku
    Jul 31, 2014 at 21:22
47

Well, if you do this:

# your code

Then import your_module will execute your code. On the contrary, with this:

if __name__ == '__main__':
    # your code

The import won't run the code, but targeting the interpreter at that file will.

If the only way the script is ever going to run is by manual interpreter opening, there's absolutely no difference.

This becomes important when you have a library (or reusing the definitions in the script).

  1. Adding code to a library outside a definition, or outside the protection of if __name__ runs the code when importing, letting you initialize stuff that the library needs.

  2. Maybe you want your library to also have some runnable functionality. Maybe testing, or maybe something like Python's SimpleHTTPServer (it comes with some classes, but you can also run the module and it will start a server). You can have that dual behaviour with the if __name__ clause.

  3. Tools like epydoc import the module to access the docstrings, so running the code when you just want to generate HTML documentation is not really the intent.

10

The other answers are good, but I wanted to add an example of why you might want to be able to import it: unit testing. If you have a few functions and then the if __name__=="__main__":, you can import the module for unit testing. Maybe you're better at this than me, but I find that my "simple scripts that couldn't possibly have any bugs" tend to have bugs that I find with unit testing.

1
  • 1
    Good point. Though my "simple scripts that couldn't possibly have any bugs" do not even have any tests in them :-) Apr 5, 2011 at 12:01
6

The if __name__ construct will let you easily re-use the functions and classes in the module in other Python scripts. If you don't do that, then everything in the module will run when it is imported.

If there's nothing in the script you want to reuse, then sure, just dump it all in there. I do that sometimes. If you later decide you want to reuse some code, I have found Python to be just about the easiest language in which to refactor code without breaking it.

1

For a good explanation of the purpose of Python's "main guard" idiom:

What does if __name__ == "__main__": do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.