Search code examples
pythonexit

How do you exit a python program without the error statement?


How do you quit or halt a python program without the error messages showing?

I have tried quit(), exit(), systemexit(), raise SystemExit, and others but they all seem to raise an error message saying the program has been halted. How do I get rid of this?


Solution

  • You are trying too hard. Write your program using the regular boilerplate:

    def main():
        # your real code goes here
        return
    
    if __name__ == "__main__":
        main()
    

    and just return from function main. That will get you back to the if-clause, and execution will fall out the bottom of the program.

    You can have as many return statements in main() as you like.