reroute terminal to interface of the application - python

I have built a small desktop application which edits data(.ags format) and then saves to selected folder. Before i had an issue that, i could run it as python file, but it would crash when I make it .exe. I figured out the problem. The reason was that, particular line of code tries to prints to terminal, but .exe did not have it. I deleted sg.output() line from code, then used pyinstaller to make it .exe. Earlier i was using psgcompiler.
Now it works fine. However, when i open software the terminal opens as well (attached photo). Is there any chance to hide it, or add it to software itself? I tried multiline.
I have tried to add, but it did not work.
[sg.Multiline(size=(55, 5), reroute_stdout=True)],
Thanks

By default pyinstaller compiles executables in console mode... which means that unless you tell it otherwise when the application is run outside of the command line, e.g. by double clicking the .exe a console window will always appear.
To avoid this simply use the windowed mode of pyinstaller with the -w flag when compiling.
pyinstaller -w myapp.py

Related

Pyinstaller .exe works from terminal but not by double-clicking -> flashing console window

When I run the .exe from my project folder from terminal with dist\app\app.exe it runs fine, I can see my output in terminal etc.
However, with double-clicking the .exe I just get a flashing terminal window.
Does anyone have an idea or a clue?
By inspiration of the following post: PyInstaller .exe file does nothing I tried using --noupx in my installer command, and this lets me app run by double-clicking while having a terminal window on the background (which I am fine with for now).
When double-clicking you are going to run your application and it will close immediately after completion. The only exception is if the application is going to ask for input. This means that your application most likely runs fine.
It is also possible that you opened cmd-line as Administrator and thanks the application runs fine but when you double-click it is not being executed because it lacks access. It is not possible to tell without a closer investigation though.

Python Executable how to on Windows

I'm running windows
I've got a python script that works as expected when I run it from my interpreter (Anaconda, i think that's an interpreter...), but when I run it from my file manager by double clicking the script ("script.py") I see a quick black screen flash, but nothing else. I'm using the input() function, so that's not the issue.
When I try the same thing with a simple test script, which is just the print() function and the input function, the command line or terminal screen, whatever the black screen is called, displays my printed string and closes only after I press enter.
I believe my issue has to do with file paths and working directories, but I'm a noob and I don't know how to solve this. My script declares this variable:
inv_folder=r"C:\Users\domin\OneDrive\Desktop\Test Folder" #folder to look for completed recipe files
which I believe is an absolute filepath. I then loop through the files in that folder and open and read them
for filename in os.listdir(inv_folder):
Main goal is to send my script to someone else's computer, and allow them to run it simply by double clicking on the file. Trying to do that on my computer and failing
Doubleclicking the .py file in Windows will generally run the file. In your case, if all your script is doing is looking for a directory and looping through files in it, it is quite possible that the script is working correctly, but that it is closing immediately upon completing the script.
E.g. if you have the following python file hello.py with the single line of code.
print("hello world")
If you doubleclick hello.py, you will only see a black window flash for a second, as the program runs and exits.
To check whether your code is working correctly or not, you are best to navigate to that folder in your command prompt and run the program within the command prompt. E.g. typing "hello.py" in the right folder will then show the correct output in the console.
Try running it in the console first, seeing if the code is giving any errors, and then working from there. But the behaviour you are describing is basically standard Windows behaviour and not necessarily an error.
If you need to pause the window after running, please see the following StackOverflow question and answers about "Python Equivalent to System('PAUSE')"
Have you considered to compile it?
You can compile it easily with pyinstaller and this command:
C:\<path_to_python>\Scripts\pyinstaller.exe --onefile -n <your script name> <your script path>
Of course replace the <> with the relevant paths for you.

How to open a tkinter script that calls a different script within the same directory?

Sorry for the confusing question. I am working on a GUI and backend of a program that takes information from the gmail-api makes it into a text file. The back end and GUI(Tkinter) are working perfectly but through the python3 shell. When I just click on the python script within the directory a black command prompt appears and then disappears.
When I click on a prototype I have of the GUI that does not call another script but a text file, the GUI pops up perfectly.
This is after I used pyinstaller to make it an executable.
My end goal is to make the whole GUI and back end an executable file.
I was wondering if anyone knows why this happens. I think it might be because of it having to call another script...but I am not sure.
Sorry again for the confusing question and wordy explanation.
You want to go to the terminal after you've intalled pyinstaller and run this command:
pyinstaller --onefile --noconsole yourtkinter.py
Explanation:
--onefile : Optional here, more comfortable for tiny tkinter projects.
--noconsole : Will solve your command line opening issue.
Find here more about the pyinstaller package:
https://pyinstaller.readthedocs.io/en/stable/usage.html

Run Python script as an exe without showing a command window

I am a security student first starting out with Python. I've built my first program for my class and it is meant to be an exe so that it may run on any computer without having me install python onto it. The program is meant to go unnoticed by the user, but whenever it is executed a command windows pops up.
Does anyone know how to make a .py into a .exe that when launched would not bring up the command prompt?
I have already tried compiling a .pyw into a .exe and it still pops up the command prompt.
Googling around the py2exe web site, you need to say setup(windows=['myapp.pyw']) instead of setup(console=['myapp.py']). I have problems finding out which page is saying that exactly, but hints are given here for example (search for "console"):
http://www.py2exe.org/index.cgi/FAQ

Can we know if a Python script is launched from Windows or a textual terminal?

I use the Windows version of Python. I have a Python script using Pyside (nothing complicated, a kind of "hello world").
When I click on my script file or if I launch it from a command line, it executes perfectly and I have a GUI appearing.
However, I would like to avoid having a GUI if the script is launched from a textual terminal (cmd.exe, cygwin, ...). A kind of script which automatically knows if it should have a GUI output or a textual output.
Is there an easy and simple way to do that? I want to be able to do that with the Windows version of Python (not the one coming with Cygwin packages).
An obvious way would be to add a kind of "--no-gui" parameter when I launch the script from a textual terminal, but I wonder if Python (or some Python libraries) already provide tools for that.
Moreover I have an SSH server (Cygwin-base) on this computer, I can execute the script at distance but no GUI appear (of course) and I have no error message. It is a case where it is very interesting to know if the script failed because of the lack of Windows graphical support or if the script should adapt its output for a textual terminal.
I know that you can run file as .py file or as .pyw file. The second option is used for GUI applications and it does not open the console window. To distinguish these to two cases you can check isatty method of sys.stdout.
import sys
if sys.stdout.isatty():
# .py file is running, with console window
pass
else:
# .pyw file is running, no console
pass
EDIT:
I tried to run that with putty+ssh on linux box - it returns True.
I tried to use msys bash shell on windows box - it returns True (.py file)
I tried to use cygwin bash shell with cygwin python - it returns True (.py file)
Unfortunately, I have no possibility to try putty + windows cygwin ssh server.

Categories