This is the biggest newbie question on the planet, but I'm just not sure. I've written a bunch of functions that perform some task, and I want a "main" function that will, for example, when I call "someProgram.py", run function1, function2 and quit. I vaguely remember something about "main" but I have no clue.
-
2+1 It's an awesome newbie question! – Paul Sasik Jan 24 '10 at 03:50
5 Answers
Python scripts are not collections of functions, but rather collections of statements - function and class definitions are just statements that bind names to function or class objects.
If you put a print statement at the top or middle of your program, it will run normally without being in any function. What this means is that you could just put all the main code at the end of the file and it will run when the script is run. However, if your script is ever imported rather than run directly, that code will also run. This is usually not what you want so you would want to avoid that.
Python provides the __name__
global variable to differentiate when a script is imported and run directly - it is set to the name under which the script runs. If the script is imported, it will be the name of the script file. If it is run directly, it'll be "__main__"
. So, you can put an if __name__ == '__main__':
at the bottom of your program, and everything inside this if block will run only if the script is run directly.
Example.
if __name__ == "__main__":
the_function_I_think_of_as_main()

- 384,516
- 81
- 508
- 779

- 37,799
- 10
- 82
- 91
-
+1. `__name__` is actually the name of the "current" module that works (not accidentally, I'm sure) conveniently for `__main__` (which is special, but still a module). This is handy combined with logging or http://stackoverflow.com/questions/2000861/retrieve-module-object-from-stack-frame/2011168#2011168 – Feb 05 '10 at 17:50
When a python module is being imported for the first time, it's main block is run. You can distinguish between being run by itself and being imported into another program:
if __name__ == "__main__":
function1()
function2()
else:
# loaded from another module

- 1,053
- 8
- 11
if __name__ == '__main__':
run_main()

- 776,304
- 153
- 1,341
- 1,358
-
This doesn't really answer the question as written, it's answering "how do I get my main function to not run when my module is imported?". If you already have a `run_main` function then all you need to do is call it. Max S's answer is preferable to me. – Scott Griffiths Jan 24 '10 at 06:46
As I read your question, you're asking about how to define a main function. That actually would be done with something like:
def main():
function1()
function2()
return 0
And then you would put code something like this outside all your main file's functions:
if __name__ == "__main__":
sys.exit(main())
(Of course you need an import sys somewhere for the above to work.)
A (now kind of old, but still relevant) post from Guido tells more.

- 18,244
- 7
- 53
- 79
-
1
-
What's the problem? It causes no error and tells the operating system that you exited without failure, thus is considered good practice in at least some circles. – GreenMatt Jan 24 '10 at 14:07
-
Sorry, you're right. I didn't read it in the context of the `sys.exit`. – Scott Griffiths Jan 24 '10 at 15:10
-
sys.exit treats None (which main returns by default) the same as 0; there's still no reason for that return statement, but +1 for the only answer to mention this important use of sys.exit. You can also return a string as a quick-and-dirty error reporting mechanism (sys.exit will print it and give a non-zero exit code to the OS). – Feb 05 '10 at 17:55