Is there any way to run a Python 3 script without the terminal or the console popping out?
I tried many ways to hide the terminal at first run but even through I used .pyw extension, included a hide() function and used the --windowed flag when converting my script to an .exe through pyinstaller, the terminal still pops out for a microsecond before disappearing.
import win32console, win32gui
def hide():
window = win32console.GetConsoleWindow()
win32gui.ShowWindow(window, 0)
return True
I've read about a method in which you could run the python script through a C program to hide the terminal before execution but I would like to keep it as simple as I can.
Do you know any way to avoid the terminal flashing out when the script run?
you can hide the console window by using the .pyw extension for the file
Related
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")
I am trying to make an executable python program on MAC OSX. I used the build applet program and it runs, but I had some data printing in the shell window and the executable file does not open a window. Is there a way to open a shell window with an executable python program?
Thanks
Not sure about generating another shell window, but do you need to have an entire shell open? What about getting the information to the user in a different way, such as:
use another Toplevel window and insert the output into a Text or Listbox, rather than simply printing it. This could also make it easier for users to copy the output (if that's something they might find useful).
write out a data/log file.
If you are using Automator to simply run your python script, and you really need it to open a shell window, here is a cheap hack:
Using a simple application with Run Shell Script action:
open -a Terminal /path/to/python/script.py
But if all your application is doing is printing output, all that output would be visible in the Console.app.
I have created a script in python and saved it as .pyw file so that I don't need console to execute the script. I am wondering how to stop the execution once it is running. What I am doing right now is opening the IDLE and closing it and it seems to work. I am sure there is a better way of doing this. Can somebody advise me about this?
As it says in the docs
The Python installer automatically associates .py files with python.exe so that a double-click on a Python file will run it as a script. The extension can also be .pyw, in that case, the console window that normally appears is suppressed.
The .pyw extension is really meant to be used by Python GUIs. Since they create their own windows, the console window isn't required. If a .pyw script doesn't create any GUI windows, killing the python process is the only way to stop execution.
If you don't want to use the console to run your script (using the .py extension), just double-click on it.
I built a tkinter app, then I converted it into a .exe. When I run the .exe, no console shows up.
But in the script, I wrote several print "stuff" statements, how can I display them when I run the .exe?
Run the .exe from the console.
Easiest way to start the console is Start -> Run -> cmd.exe
Then cd to the directory where the .exe is, and run the .exe.
Edit: If you used py2exe, you may need to pass your script to setup() as the console argument:
setup(console=['hello.py'])
This will tell it you want console output. That's from the py2exe tutorial.
Also see Hiding console window of Python GUI app with py2exe, and do the opposite, since you don't want to hide the console output.
So I want to make a Python file that runs all the code within, but the window is invisible. So the user won't see the window in the task bar, or really anywhere on his screen.
How would I do something like that?
To make a script not open the terminal window change the extension of your script to .pyw which will cause the script to be executed by pythonw.exe by default. This suppresses the terminal window on startup.
If you would like all scripts to open like this you should read up on Executing Scripts.
Maybe you'd want to use start pythonw.exe