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

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.

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.

Kivy, how to disable console

I'm coding some software with python and kivy and want to disable the command window, that opens up in window side by side with the kivy app.
I entered that command in the command window to call the python prog:
python main.py -m console
From now on, I can click once at any kivy widget on the bottom side of my app. Then there is that console showing in the picture below. I can disable it by pressing STRG+E twice (activate, then deactivate).
The bad thing, this console part is there with every program start out of eclipse, direct call by python main.py or also in the compiled version, compiled with pyinstaller.
I tried to reinstall whole python on my system, but it doesn't work.
How can I disable this console thingy?
If you're packaging your app with PyInstaller, you should take a look at the --noconsole command line option. It should get rid of the console in Windows and OS X:
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.
import sys
if sys.platform=="win32":
import ctypes
ctypes.windll.user32.ShowWindow( ctypes.windll.kernel32.GetConsoleWindow(), 0 )

Hide the console of an .exe file created with PyInstaller

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.

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

Opening a file, and taking input from the file opened in a Python program

I have written a program in python that is another front end for un-tarring files on OSX. I want to be able to double click on the file and open with the program that I wrote. How do I make the program automatically know that it is being opened with that file, and to work on that file?
You will need to make your make your script executable with the command chmod a+x <your script.py>
You will also need to tell your OS that the python interpreter is needed to execute this file. In Linux the line #!/usr/bin/env python at the top of the file is what does this. I assume its the same in OSX.
Then right click and select "Open with" and then "Other...". And select your script.
The Python script will need to be configured correctly to run when the OS calls it in this way.
Another way to do it(Caveat: I haven't personally tried this) is to use a shell script as advised in this anwer on the superuser forums.
Python scripts on OS X are not by default recognized as launchable applications by the Finder. What you need to do is use a tool that will package your Python script as a Mac OS X application. Then, you can right-click or ctrl-click a file of your chosen file type and locate that application through the Open With... dialog.
One option I'm aware of for doing this is part of the MacPython package, called py2app:
http://wiki.python.org/moin/MacPython/py2app
So, your steps would be:
1) Use py2app to package your Python script as a launchable application
2) Associate the application with the file type you'd like it to open using by right-clicking or ctrl-clicking one of that file type and choosing "Open With..." in the OS X Finder.
import sys
print("Program.py executing...")
for x in sys.argv:
print(x)
Produces the output
Program.py executing...
Program.py
something.tar
I'm windows, running this command:
python Program.py something.tar
I don't think anything changes from that command to OSX.
And as said by benjamin, the OS is responsible for determining what program is used for the file. I don't know what the command line from your os will be, but you can find out with the above program.

Categories

Resources