简体   繁体   中英

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?

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.

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM