Command window popping up when running a Python executable? - python

I recently made an executable of a Python program with py2exe, and when I ran the executable, a command window appeared for a split second and then disappeared. My program itself never actually ran at all. Everything is still inside the dist folder, so I'm not sure what's actually wrong. Is there a solution for this?

If all your program does is print something and you run it by double-clicking the executable, then it simply closes the console when it finishes running. If you want the window to stay open, run your program from the command line. You can also create a batch file that runs your program and then pauses the console, so that you at least get a "press any key" before the console closes.

Related

Pyinstaller executable is crashing?

I'm attempting to use pyinstaller to make an executable for my python program.
I've already tried the following commands, pyinstaller --debug --onefile run.py, pyinstaller --onefile -w run.py.
When run with the debug option the program starts up and starts finding the necessary imports for the program. It then executes a print statement I placed at the beginning of the program. Immidately after the print statement it starts running destroy statements. Then the command window closes and the program never opens.
When run without the debug option the program loads for about ten seconds then a windows message box appears with the error 'Failed to execute script run'. This message box does not appear with the debug option disabled.
I am using tkinter for this program. Had a couple thoughts that tkinter may be the problem but no real evidence, just a hunch. Any advice is welcome, thanks.

Python.exe opens in a new console window

I used to run Python scripts from my Windows command line, and all the prints were printed in the same console. Now something happened on my machine (Windows 10), and when I launch a Python script from the command line (i.e. open a Command Prompt and run python <my_script.py>), Windows opens a new window (titled with the absolute path of python.exe). This windows closes automatically at the end of the execution, so that I can't see the output.
How do I go back to printing output in the same command prompt window from which I run the script?
Not sure how useful this will be but I had this same problem, found this thread, and realized that the new console window was opening up when I omitted 'python' from the command.
>python myscript.py
shows the output right in the terminal where I typed the command, but
>myscript.py
opens the new console window and closes it immediately after the script runs.
It's odd but it very likely a windows setup issue as python is an exe. If memory serves windows will spawn on a > run command so checking the way python is booting will help.
Unfortunately it could be a range of issues, so some steps towards victory:
What happen when you just type python into the cmd? If it simply starts the input >>> - it means your python setup is fine. If a cmd window spawns and disappears it may be a windows permissions issue.
Try running your script with -i flag: python -i script.py. This drops you into the repl when the app completes - displaying your output.
Ensure you're using the native flavour of the cmd to test. Ensuring any command app or IDE isn't injecting a start command or weird /K (spawn new window) flag.
Hope it helps.
In my computer this was caused by Windows not knowing what program a .py file was associated with. I solved this by going to:
Control Panel -> Programs -> Default Programs -> Associate a file type or protocol with a program (Scroll down) and choose "Choose default apps by file type" Scroll down until you see ".py" and choose the correct
Python interpreter.
Simply: last row on the end of your program maybe this:
input("\nIf you whish end the program press any key ...")
...and your program wait for the key and you see your outcome

How to make the text shell not appear when running a program

I'm just wondering (as im starting to make a game with pygame) is there a way to 1) Run a program that only opens a GUI and not the shell
and
2) Run the program without having to edit with idle (I'd like to do this so the game will look more professional.
I would like to just have a desktop shortcut or something that when clicked, runs only the GUI and doesnt show my code or show the shell.
If you're on windows, a quick thing could be to make a one-line batch script.
start pythonw.exe <filename>
The start keyword causes the shell to not wait for the program to finish.
pythonw vs python prevents the python terminal from appearing.
If you're on linux/mac, you could do the same with shell scripts.

Python script displays output in command window but nothing shows in Windows

I've written a script that works great at a command prompt, but it only displays the last line if I double click on the script.py icon on my desktop.
The function in the script runs perfectly but once it finds a match it's supposed to display the output on the screen. At the end, I have an os.pause statement and THAT displays when the script is done, but nothing else displays on the screen.
I AM executing it using pythonw.exe. What else should I check?
Thank you.
pythonw supresses the console window that is created when a python script is executed. Its intended for programs that open their own GUI. Without pythonw, a python gui app would have its regular windows plus an extra console floating around. I'm not sure what pythonw does to your stdout, but os.isatty() returns False and I assume stdout/err are just dumped into oblivion. If you run a DOS command like os.system("pause"), Windows creates a new console window for that command only. That's what you see on the screen.
If you want to see your script output, you should either run with python.exe and add an input prompt at the end of the script as suggested by #GaryWalker, or use a gui toolkit like tcl/tk, Qt or wxPython.
I've used a script like before and it seemed ok to me
print("Hello world")
input("Press return to exit")

After showing the output the python window closes immediately

Python version 2.7
I tried a code and it was not giving any error. When I ran the program, It showed the result only for a few milliseconds and then the window closed immediately. Is there any method to display the result permanently and to close the window only at my wish rather than it closing all by itself?
Just use an raw_input() at the end of you script. This will prompt for a string, and you can close the window then by pressing "Enter"
I am assuming that you are running it by double clicking your .py script. Your OS (Windows I assume) will then close the calling 'cmd' terminal once the script has finished, you could circumvent this by starting the terminal manually (just goto start -> execute and enter cmd) to run the script so it won't close automatically.
This happens because your code runs as fast as it can and stops when its finished.
When you open a .py program via double clicking it on a windows OS; it opens the program in a cmd window and when the code is done running it closes the window again. You only specified it to perform some calculation and then stop after that. Therefor in order to keep the program running type raw_input() or if using python 3 input() at the end of your code in order to prevent it from running all the way through until you press a key.

Categories