What does this mean exit (main())

Question:

I have seen this in a couple of python script I have been reading lately. I have looked at the doc; they only give examples deal with passing a termination value or string of some sort.

I think what this does is call the main method, then exit?

If someone could shed some light on this I would appreciate it.

if __name__ == "__main__":
   exit (main())

Please and thank you

Asked By: myusuf3

||

Answers:

This will call the function main() and when main finishes, it will exit giving the system the return code that is the result of main().

A simplified example where this might be used:

def main():
    try:
        doSomething()
        return 0
    except:
        return 1


if __name__ == "__main__":
    exit (main())

If an explicit return value is not given in main(), the default value of None will be returned. This produces the same system return code as explicitly specifying return 0. If main returns anything other than an integer or None a system return code of 1 will be produced.

Answered By: amicitas

It means run the main() function and exit with the return code returned by the main() function. It’s a common idiom so that, when running a script from the shell, you can reliably tell if it suceeded.

The if __name__ == '__main__': idiom is a common way of running code only when a module is run as a script (as opposed to being imported).

Answered By: Rafe Kettler

Because if main() contains a call to sys.exit() it will exit the interpreter.

The reason for doing this is to use the return value of main() as the scripts return code.

Answered By: vicTROLLA

If you execute a Python script directly, __name__ is set to "__main__", but if you import it from another script, it is not.

So in this case, the script is seeing if you’re executing it directly. If it is, it calls the main() function to perform some work, and returns the return value of the main() function to the system via exit(). If the script is being imported from another module, it doesn’t execute the main() function and simply provides the script’s functions and classes to the importing script.

This is a common idiom in Python. It allows you to have scripts that are standalone programs, but can also be imported without trying to do work that the importing script doesn’t want done.

Answered By: Chris Lutz

Just adding an example to the type of scripts mentioned in the question:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import sys

from youtube_dl import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script.pyw|.exe)?$', '', sys.argv[0])
    sys.exit(main())

This is from youtube-dl package where another main function is imported from youtube_dl module. Note this is not to confuse with “__name__ is set to __main__” when executing script directly as @Chris pointed out. Overall, exit(main())
could be utilized to make the script run neatly.

Answered By: Marco
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.