1

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.

GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
Milano
  • 18,048
  • 37
  • 153
  • 353
  • Define "do nothing". So if you compile `print("a"); sys.exit(); print("b")` into an executable and run it, it will print both "a" and "b"? – Kevin Dec 17 '15 at 18:06
  • It prints 'a'. There is no 'b' ... I'm on windows. – Milano Dec 17 '15 at 18:10
  • In that case, `sys.exit` is working as intended. It makes the program stop running before "b" gets printed. Can you explain further exactly what else you expect it to do? – Kevin Dec 17 '15 at 18:12
  • I want to close the console window. Thank you for explanation. – Milano Dec 17 '15 at 18:13
  • Maybe [How to hide console window in python?](http://stackoverflow.com/q/764631/953482) will be useful to you, then. – Kevin Dec 17 '15 at 18:14
  • @MilanoSlesarik did you ever find a solution? am running into the same problem. Pls share whatever worked for you, thank you! – James Wong Nov 06 '18 at 03:21

4 Answers4

3

Use: quit() In python's command prompt

Cirilo
  • 31
  • 2
2

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 :)

Mark
  • 780
  • 1
  • 6
  • 17
0

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.

Aeolus
  • 996
  • 1
  • 8
  • 22
0

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')

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – user11717481 Feb 22 '22 at 14:47