Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  1. 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
  2. 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?

like image 539
Alan L Avatar asked Aug 16 '16 17:08

Alan L


2 Answers

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.

like image 140
Peter Avatar answered Oct 20 '22 03:10

Peter


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:

  1. hellow.exe for the GUI version (windowed) and
  2. 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..

like image 36
Michael Altfield Avatar answered Oct 20 '22 05:10

Michael Altfield