Search code examples
pythonexecutablecx-freeze

Is it possible to package a python application with its separate terminal?


I would like to have an executable (Windows, MacOS, Linux) of my program that will have its own terminal window where users will be able to type in. I don't want to have the user install modules or anything except this executable. You could say that the UI of my program would be a terminal window. Is it possible and with what?

I basically want the GUI of my app to be a terminal window.


Solution

  • To create a command-line interface for your program you'll likely want to use something like argparse, which is in the standard Python library. There are many other feature-rich command-line interface modules (e.g., click) you could try to use instead. You can design command-line interactions for your users including input and output, menus, etc.

    Once you've built your application, a good choice to make it portable is the PyInstaller module. If you set the --onefile option when invoking it, it will generate a single .exe file which can be easily shared with users. If you also set the --console option when invoking PyInstaller, then your Python .exe application will interface with stdin/stdout. On Windows, this would be a cmd window.

    If a user invokes your .exe via the GUI (e.g., a double-click on its icon), a Windows CMD window will be opened. If you want that window to stay open, however, you will need to create your application with some kind of "infinite"/"captive" design that never finishes executing until the user explicitly quits.

    If the user instead runs the .exe from a Windows CMD terminal that is already open, then that window will always stay open.