How to properly close Python exe console program - python

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.

Use: quit()
In python's command prompt

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

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.

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

Related

.exe file opening Powershell Window

I made an .exe file using pyinstaller, but when I run the file it opens a PowerShell window as well. I was wondering if there is anyway I can get it to not open so I just have the python program open.
I haven't really tried anything as I don't really know what I'm doing.
When running pyinstaller be sure to use the --windowed argument. For example:
pyinstaller –-onefile myFile.py –-windowed
if you run it from terminal, you can use this command:
start /min "" "path\file_name.exe"

Failed to execute script

I wrote a simple script in python 3.7 and I coverted it in .exe from cmd with the following line pyinstaller -w -F -i [myicon.ico] [myscript.py] . My script works if i launch it from command line (python myscript.py ), but it doesn't work when i click on "Myscript.exe" (I have a window with this message :"Failed to execute script Myscript").
I try to create the same script without the icon but result is the same.
Someone can help me?
You can refer to that post:
Windows- Pyinstaller Error "failed to execute script " When App Clicked
Actually the answer is that python doesn't compile the icon and so you need to manually transfert the icon in the folder containing the .exe.
B

Script doesn't produce any output after being converted to Win executable

I have a python (3.6) script prints the output using print() command:
print(convert_size(logsize))
Then I converted the script to .exe using cx_Freeze 5.0.1. When it does work from IDLE and prints the output, launching .exe file with double-click, or as Administrator, or executing it from cmd as Administrator doesn't produce any output at all: http://prnt.sc/emz5m4
I have tried to add input() at the end of the script and then re-compile the file to .exe, which supposed to "stop" window from closing but it still closes.
Does this happen to you only when compiling this script, or also with other scripts?
If you can't manage to get outputs into a console with cx_Freeze, maybe you can try compiling with pyinstaller. Specifically, the options '--console' or '--noconsole' will give you control over whether a console opens or not to see the outputs. It has worked fine for me.
https://pythonhosted.org/PyInstaller/usage.html
It appears the pyinstaller has been launched from 3.6 location since I had it installed for 2.7 and 3.6. Once the correct location (C:\Python27\Scripts) has been pointed to, the script has been compiled successfully!

Getting pyinstaller .exe program to run from clicking file?

I have a command line based program that I've turned into an executable using PyInstaller. I would like the program to launch a command prompt and run when clicked. Currently I can only get it to run from the command prompt. When I click it, a blank command prompt will open, remain open for a couple of seconds and then close. I'm stuck. Does anyone have any suggestions?
Have you tried the --console flag with pyinstaller?

Converting .py to .exe in python 2.5: doesnt execute .exe program directly

I have successfully converted my .py script to .exe but the problem is that it wont get executed when I press the .exe icon. A black screen pops up for a fraction of a second and then disappears.
I tried executing it through the command prompt and it did get executed but doesn't run if I click the .exe directly. Just want to know if there is some error in placing the .exe file due to which I am encountering this problem.
# setup.py
from distutils.core import setup
import py2exe
setup(console=["myscript.py"])
python setup.py py2exe --help
python setup.py py2exe
Your shell disappears because the program finishes.
End your program with a raw_input('enter to finish') line and the console will stay allowing you to see the result.
How do you know it got executed when you ran it from command prompt? If the app just outputs something and exits, you will see the behavior you are describing: the black window will flash and disappear. That the same as running any other win32 console app from windows explorer.

Categories