Hide the console of an .exe file created with PyInstaller - python

I want to make my program executable.
I used TkInter to write the GUI, and I read somewhere that you have to save your file as .pyw to hide the console when the program is executed.
The problem is that after making it an executable with PyInstaller, the console shows up again, even though the file converted was .pyw.
How can I hide the console also in the .exe file?

Did you try --windowed command line flag ?

You can just save the file with .pyw extension and you can just use pyinstaller --onefile Filename.pyw
I think you renamed it. You save it as .pyw don't rename it.
The screenshot are below:
Output:
But it takes a while to open.
Thank you
-Levers

What are you using to make the executable?
If you use py2exe and you use:
setup(windows=[pythonscriptnamehere])
in the setup script instead of:
setup(console=[pythonscriptnamehere])
it will run the executable without launching a terminal in the background.

From The PyInstaller Documentation:
Using a Console Window
By default the bootloader creates a command-line console (a terminal
window in GNU/Linux and Mac OS, a command window in Windows). It gives
this window to the Python interpreter for its standard input and
output. Your script’s use of print and input() are directed here.
Error messages from Python and default logging output also appear in
the console window.
An option for Windows and Mac OS is to tell PyInstaller to not provide
a console window. The bootloader starts Python with no target for
standard output or input. Do this when your script has a graphical
interface for user input and can properly report its own diagnostics.
As noted in the CPython tutorial Appendix, for Windows a file
extention of .pyw suppresses the console window that normally appears.
Likewise, a console window will not be provided when using a
myscript.pyw script with PyInstaller.

Rather than saving/renaming/changing the extension to .pyw, you can just add --noconsole to the command line and use the standard .py file with it and this would hide that console window.
Example:
We have a file named GUI_name.py in our folder that uses Tkinter. Now we could easily create .exe file that doesn't show the console window by typing pyinstaller --onefile --noconsole GUI_name.py in cmd.

Related

How to hide cmd console when excute .exe program compiling by Nuitka?

I used this (python -m nuitka --follow-imports program.py) command to compile my script using Nuitka . It made an .exe file which worked quite well for me other than --onefile commands , as they were showing me import error. But here the problem is , a cmd console opens up of my .exe file after is start it, then my tkinter gui console shows up. I tried to use .pyw file to compile my script, but it didn't help me. I need help to shutdown or hide that cmd console window which pops up for my .exe file.

How to printout a help to terminal window from compiled .exe file?

I'd like to show short description about program for users.
I have .py file compiled to .exe by pyinstaller and try to use the following code to show help if any arguments were passed to the .exe file from terminal window.
if len(sys.argv) > 1:
# argv[1] has your filename
sys.stdout.write("Description about program...")
sys.exit("Terminating")
If any arguments passed to .exe the program finishes by sys.exit command, but no printout shown in terminal window.
I used -w flag with pyinstaller when generated .exe.
That was the clue, it suppresses console output even if you launch the program through terminal window.
Thank you everybody.
-w, --windowed, --noconsole
Windows and Mac OS X: do not provide a console window for standard i/o. On Mac OS X this also triggers building an OS X .app bundle. This option is ignored in *NIX systems.

Making a PyInstaller exe do both command-line and windowed

I am writing a Python program that can be used both on the command-line, and as an interactive window. (Is that a bad idea?) If command-line arguments are supplied, it executes a task, then prints "success" or "failure". Otherwise, it launches an interactive window.
PyInstaller doesn't seem to be built to support this. I have two non-optimal options:
Use --console mode: The command-line works great, but if I double-click the exe to show the interactive window, it also shows a console window that I don't want
Use --noconsole mode: There's no console popup, but no output shows when using the command-line.
It seems I either need a way to not pop-up the console in --console mode, or to show print output in --noconsole mode. If neither of those options work, I may need to make a separate command-line version of the program.
Any advice?
This is not a perfect solution, but this workaround did the job for me:
Build gui app in --noconsole --one file mode like this:
pyinstaller --noconsole --onefile hello.py
When you double click on the app from windows it will launch normally (without the console).
Now to see the output, navigate to the executable from the command line and type:
hello.exe | more
The "| more" should send the print statements to the console.
This is a problem with Windows (not PyInstaller), which requires the subsystem to be specified as either CONSOLE or WINDOWS at compilation-time.
https://github.com/pyinstaller/pyinstaller/issues/6244#issuecomment-927131015
The recommended solution is to split your app (eg hello) into two distinct versions:
hellow.exe for the GUI version (windowed) and
hello.exe for the CLI version (console)
In theory, you could also add a wrapper .exe that switches between the two actual binaries above, depending on how it's called..

can't execute pyw on windows

I want to hide the console window of a python program, so I change the file extensions to "pyw", but when I open it, the python IDLE show up even though I choose open it with "pythonw.exe"
If I use "pythonw test.py" in cmd, it works.
So I want to know what's wrong with this and how to solve this, thank you.
Change the program that opens python files.
Assuming you're using Windows, right click any python file (in your case any .pyw file, not .py), properties, change Opens with to pythonw instead of IDLE
For me, I had multiple version of Python installed that was causing issues. Once I had only had one version, I applied that pythonw.exe was the default for .pyw files and it worked.

why is the python GUI interface fleeting

#coding=utf-8
import wx
class App(wx.App):
def OnInit(self):
frame=wx.Frame(parent=None,title='Bare')
frame.Show()
return Ture
app=App()
app.MainLoop()
runs OK! but the GUI interface is fleeting, just leaving the CMD console in the screen.
a newer for python ,why the outcome of the GUI interface fleeting?
environment:Gvim+WIN7+PYTHON2.7
First, according to help :! command of vim:
:!{cmd} Execute {cmd} with the shell.
vim cause the cmd shell to be executed.
Second, according to Using Python on Windows - Executing scripts:
Python scripts (files with the extension .py) will be executed by
python.exe by default. This executable opens a terminal, which stays
open even if the program uses a GUI. If you do not want this to
happen, use the extension .pyw which will cause the script to be
executed by pythonw.exe by default (both executables are located in
the top-level of your Python installation directory). This suppresses
the terminal window on startup.
So, if you want run the GUI program without cmd console, run the program outside the vim using pythonw.exe. For instance, save the file with .pyw extension, and double click the file.
It is fleeting because you have a NameError in your code. You should be returning True, not Ture, which is undefined.
I am surprised you still see the console window though. If that stays up though, it should be showing the traceback.

Categories

Resources