Milano
Milano

Reputation: 18705

How to properly close Python exe console program

I'm trying to figure out how to do exit if the program is done with it's work. What should I use?

I've tried:

sys.exit()

and

os._exit(0)

But none of them worked in exe created by Py2Exe. It works when it is runned as a py script but if I create an exe, those exit commands do nothing.

Upvotes: 1

Views: 15181

Answers (5)

python_smaxx
python_smaxx

Reputation: 11

Using simply exit() or quit() might not work as it might be undefined in some runtime environments Use: sys.exit() to terminate the program explicitly

Upvotes: 0

Joaquin Bracci
Joaquin Bracci

Reputation: 1

If you are trying to execute a command from cmd on python just add \c to the command, and it will close after its execution

example os.system(r'cmd /c "C:\Program Files\PATH" -file C:\PATH.exe')

Upvotes: 0

Mark
Mark

Reputation: 810

Use pyinstaller instead of py2exe you will love it :). sys.exit() should work on pyinstaller.

to install

python -m pip install pyinstaller

to build exe in single file

pyinstaller yourscript.py --onefile

to build a gui app w/o console

pyinstaller yourscript.py --noconsole

If you have problem using pyinstaller dont hesitate to ask :)

Upvotes: 2

Cirilo
Cirilo

Reputation: 31

Use: quit() In python's command prompt

Upvotes: 3

Aeolus
Aeolus

Reputation: 1076

If you are just using a console window, it will automatically close at the end of your program.

print("Hello World") input()

If you run the .py file, a terminal window should pop up that says hello world. When you hit enter(the input part) the program will look for another line of code and when it finds none, will close the program and console window.

Upvotes: 0

Related Questions