10

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.

1
  • 2
    +1 It's an awesome newbie question! Commented Jan 24, 2010 at 3:50

5 Answers 5

12

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()
Sign up to request clarification or add additional context in comments.

1 Comment

+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 stackoverflow.com/questions/2000861/…
3

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

Comments

1

It's the following idiom:

if __name__ == "__main__":
   yourfoo()

Also read this.

Comments

1
if __name__ == '__main__':
  run_main()

1 Comment

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.
1

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.

4 Comments

return 0 ? This is Python, not C :).
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.
Sorry, you're right. I didn't read it in the context of the sys.exit.
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).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.