I have a python script that I compiled into a Ubuntu Executable. However when I run the executable nothing happens I'm using pyinstaller, and I know its working because another project (That uses tkinter) I did executes in the tkinter window, I guess the question I have is how do I get the compiled python code to run in the terminal instead of a tkinter window.
I compiled both projects the normal way with pyinstaller, but the does that used a tkinter window runs just fine, but the code that should print to the terminal (after asking for the location of a file via askopenfilename) doesn't run at all.
Related
I have a python script that runs without problems when I run it from the Windows command shell or Visual Studio 2019. When I run it in the Spyder IDE, executing the TCL script hangs. I'm using the following to run the TCL script:
subprocess.call("script.tcl")
Is that script running with tclsh or wish? Only the former really works properly as a subprocess thing on Windows because tclsh is built as a console program and terminates when the end of the script is reached, whereas wish is built as a GUI program and does not (it instead runs the event loop until the last window is closed).
The default binding for .tcl files on Windows is against wish. I'm not convinced that that's the right choice, but it's how it is.
I'm asking help today because I'm new to Tkinter and Pyinstaller (and python in general) and I'm having troubles with it.
I have a simple app working with sqlite, tkinter and pyinstaller to compile all of this in an executable program, the entrance point of my program is a file named main.py
This file calls all the dependancies (like the sqlite module for python, tkinter and my other files like classes etc...)
I made a very simple interface, with a Hello World in a tkinter label and a button to go to page 2 which displays page2 (also in a label), just to see if I'm capable of making it all run and compile all of these pieces together.
I can run it throught my shell executing it like : python main.py and everything is working fine.
But when I run pyinstaller on my linux machine, and start executing the program, nothing appears, my database.db (sqlite database file) is created but I don't have any interface like when I run it with my shell. The thing is getting even worse on windows where, once I've my .exe it just opens a shell and crash after few seconds, not even creating the database.
What I did is I created a 'log file', in which I write the steps of the program.
As you can see on the following picture, the 2 first prints are wrote in my log file (on linux), so I think it crashes when I try to create the window.
If any of you have an idea on what I do wrong, I would really appreciate help :)
General
From the PyInstaller manual:
Before you attempt to bundle to one file, make sure your app works correctly when bundled to one folder. It is is much easier to diagnose problems in one-folder mode.
As the comments suggested, use a catch-all try/except block to log all exceptions to a file. That is probably the best way to see what is really happening. Make sure that the logfile is created in an existing location where you have the necessary permissions.
I would suggest to take advantage of the built-in logging module instead of creating your own. It can e.g. automatically add from which file a log line was created.
IMHO, it is probable that the failures on Linux and ms-windows have completely different causes. You should probably treat them as different issues.
Linux
When you use single file mode, that file is unpacked into a temporary folder, probably somewhere in /tmp. Some Linux distributions mount the /tmp filesystem with the noexec flag. This is incompatible with PyInstaller.
ms-windows
On windows, there are basically two different Pythons; python.exe and pythonw.exe. Basically it is one of the quirks of windows that this is necessary. The latter is for GUI programs like tkinter programs. A tkinter script should not show a cmd window. So I'm guessing that PyInstaller calls your command with python.exe instead of pythonw.exe. From the manual:
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.
Also, on windows it can matter which Python distribution you're using. I used to be a fan of Anaconda, but lately I've come to prefer the python.org version because it gives me less headaches. On anaconda Python I had the problem that tkinter programs would not launch without showing a cmd window, whatever I tried. Only switching to python.org Python solved that problem.
Code in question:
from tkinter import *
root = Tk()
test_label = Label(root, text = "Hello")
test_label.pack()
root.mainloop()
I can only run this python code from the IDLE, if I run it any other way the window flashes on the screen and closes immediately. I've tried:
-adding an "input" line to keep it from closing
-running from the windows powershell
-compiling the code into an EXE with pyinstaller
and now I can't find any other suggestions. I tried making a simple 1-line program that just asks for input, and that works normally and stays open fine. The tkinter program works fine in IDLE but not any other circumstance.
What's happening here?
EDIT: If I run the program from the command line instead of windows 10 powershell, I get the following output:
Traceback (most recent call last):
File "C:\Users\Cam\Desktop\CSCI Notes\Programs\test.py", line 1, in
<module>
import tkinter
ImportError: No module named tkinter
However, the tkinter file is in the python library on my computer, and importing tkinter in python shell or IDLE works fine.
When I run python on command prompt, it shows python 2.7, so I changed tkinter to Tkinter and the program worked by importing the .py file
Using cmd:
Pyinstaller works(through canopy cmd prompt):
the first line is what I put into my IDLE's cmd prompt
Which populates the folder when executed:
And inside dist, I run test.exe which shows the window
NOTE: my system command prompt is using python 2.7 while the canopy(python environment) command prompt uses 3.5 with pyinstaller
Okay, I think I've solved this one! I read that windows can sometimes try to open .py files with python 2.7 instead, even if they're written in 3.6. I uninstalled python 2.7 from my computer, and now the file runs normally.
So for anybody having this problem, try to make sure your computer is opening python 3, not python 2.
I have also had this problem and as far as I can see no one has a proper solution.
The best I have found is putting -i before your filename. For example:
python -i yourfile.py
this will start the IDLE in the command line
This could also happen if you are using a modulo that isn't installed in your computer even if you have installed it using your IDLE. Try installing it with the command "pip install modulo" else try installing it manually.
So I'm new to coding and I'm currently learning Python with the intention of creating a basic GUI for running test software and automating equipment. What I can't seem to understand at this stage is how I will go about launching my python code. As it stands now I run my basic code from the terminal or IDE (I'm on Mac OS X), but when I have a working program with a GUI I want myself and others to be able to just click and run the program as you would any other application. Can I make it so someone could just double click the .py file and the app would run and no code or terminal would be visible, just the GUI? Or do I have to make some sort of executable file for each OS? Worth noting that eventually the final product will be running on windows xp.
I recently tried to convert one of my python scripts (used for file-systems I/O) to a executable file by using py2exe. However, after successfully generating the .exe file from my python script, I am no longer able to copy and paste any text(or anything for that matter) from other windows apps to my python app console (when I run the script/app from the .exe file). When right click the mouse now, the access window does not pop up any longer. Does anyone know how I can get around this issue?
Thanks,
A.L.
Right-clicking a console window is a "special feature" of the command line interpreter of Windows Vista and above. It doesn't work on any other command-line tool, so it doesn't longer works for your converted script. You can still access it using the windows menu (small icon in the title bar or Alt+Space).
You could try to get the right-click feature back by messing around with Windows API calls - but the more convenient possibility would be to enable pasting using Ctrl+V and forget about the window menu. A good candidate is PyReadline - install the package and run this at the beginning of your script:
import readline
readline.parse_and_bind("control-v: paste")