Help with Tkinter in py2exe - python

I'm trying to convert a basic tkinter GUI program to an .exe using py2exe. However I've run into an error using the following conversion script.
# C:\Python26\test_hello_con.py py2exe
from distutils.core import setup
import py2exe
setup(windows=[r'C:\Python26\py2exe_test_tk.py'])
C:\Python26\py2exe_test_tk.py is the following code
import Tkinter as tk
root = tk.Tk()
root.title("Test")
label1 = tk.Label(root,text="Hello!",font=('arial', 10, 'bold'), bg='lightblue')
label1.pack(ipadx=100, ipady=100)
root.mainloop()
This is the error I get when I try to run the newly created .exe
Traceback (most recent call last):
File "py2exe_test_tk.py", line 4, in <module>
File "Tkinter.pyc", line 1643, in __init__
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
{C:/Users/My_Name/lib/tcl8.5} {C:/Users/My_Name/lib/tcl8.5} C:/Users/lib/tcl8.5 {C:/Users/My_Name/library} C:/Users/library C:/Users/tcl8.5.8/library C:/tcl8.5.8/library
This probably means that Tcl wasn't installed properly.
I'm pretty sure it's something in my conversion script thats giving me problems. What did I omit? Or does someone have an example of what the conversion script would look like for a tkinter GUI program? Also is it possible to divert the output .exe files to my desktop?
EDIT:
The error report said that I was missing init.tcl from {C:/Users/My_name/lib/tcl8.5}. So i made that directory and put a copy of init.tcl there. Now when I try to run the .exe it states that MSVCR90.dll is missing from my computer and is needed to run my program.
Also this is python 2.6.5 on Windows 7.

Such errors in Unix world are usually due to incorrect PATH settings or/and incorrectly installed third party modules (the GUI ones you're using). Have you seen this post: py2exe fails to generate an executable ?

Related

Pyinstaller to package app hidden-import warning/exceptions

So, I've had some trouble setting up my application through Inno setup Compiler, at first I assumed it was a problem within Inno itself but on further inspection I think it is my actual exe. I am able to create a working exe file that runs my program properly but only on my own pc. I am able to create the setup file that also works through Inno setup but it only works on my own pc. I have sent both the actual exe file and the Inno setup file to another computer and downloaded it there and ran it, both meet the same "Fatal Error: failed to run script tk_app.py". Therefore, the problem must be that I have not been able to pavkage the app properly with pyinstaller.
My prgoram has 5 files in total (all in the same folder): main.py, file1.py, file2.py file3.py, tk_app.py
All of them importing each other and using python libraries. I know that pyinstaller supports librarires such as pandas, tkinter and many more without needing the --hidden-impoort= command and that it will pick up all files within the program if there are files that are importing each other.
So I use the file tk_app.py (which contains my tkinter UI and imports main.py which then goes onto import file1.py which import another file so on)
The pyinstaller command line I use to make the exe is as follows:
PS C:\Users\ripta\Desktop\CODING\CSV_Upload> pyinstaller -w --hidden-import=bs4 --hidden-import=fake_useragent --hidden-import=urllib.prase --hidden-import=urllib.request --hidden-import=os --hidden-import=pandas.io.parsers --icon=trademark_icon.ico --onefile tk_app.py
My question is, will pyinstaller tell me when it needs a given --hidden-import='name' when running becuase it doesn not throw up any errors and does produce a spec file, a build folder and a dist folder containing the exe file.
I have noticed that it throws up WARNINGs or Exceptions (also not sure why it mentions django as I do not import or use it at all inthe application) :
59182 INFO: Loading module hook 'hook-django.db.backends.py' from 'c:\\users\\ripta\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages\\PyInstaller\\hooks'... 61711 WARNING: Hidden import "django.db.backends.__pycache__.base" not found! 61712 INFO: Loading module hook 'hook-django.py' from 'c:\\users\\ripta\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages\\PyInstaller\\hooks'... Traceback (most recent call last): File "<string>", line 21, in walk_packages File "c:\users\ripta\appdata\local\programs\python\python36-32\lib\site-packages\django\contrib\gis\admin\__init__.py", line 5, in <module>
Or show Hidden-import not found (Of imports I have no idea about):
149329 WARNING: Hidden import "pkg_resources.py2_warn" not found! 149330 WARNING: Hidden import "pkg_resources.markers" not found!
The fact that the script fails to run on any other computer besided my own leads me to think that it must be lacking a dependency that is only found on my computer, therefore I am not using pyinstaller correctly but am not too sure where exactly I am making the mistake.
First I've uninstalled Setuptools and reinstalled it with specific version. Then, I've imported pkg_resources.py2_warn as hidden import :
pip uninstalled setuptools
pip install --upgrade 'setuptools<45.0.0'
pyinstaller --hidden-import='pkg_resources.py2_warn' tk_app.py
It worked for me.

Creating Python Tkinter exe file with Pyinstaller problem

I have created GUI python2.7 program with Tkinter successfully without error. Now I want to make an executable file of it using pyinstaller in anaconda environment (I'm using windows 10).
Using this command
pyinstaller --onefile main.py I am able to create exe file successfully in dist folder. But when I tried to run the exe file, it showed error :
Traceback (most recent call last):
File "main.py", line 333, in <module>
File "main.py", line 90, in __init__
File "lib-tk\ttk.py", line 715, in current
_tkinter.TclError: Index 0 out of range
[22668] Failed to execute script main
Is the problem related to tkinter? I've tried the solution here : Problems with Pyinstaller with tkinter app on python 3.5 and here : How to make pyinstaller import the ttk theme? . But still same error
Try and do:
pyinstaller --onefile -w main.py
The -w flag stops python from bringing up the console, and so this could be why tkinter is failing.
Source:
This cool video
I highly recommend you watch this video as it also goes a little in depth on how to clean up after building the exe.
I used pyinstaller to create single exe file for a tkinter project, which contained several py files:
Main.py is my main python file which calls tkinter UI and I had several other python files imported to this Main.py.
Some libraries which I used include pandas,pyodbc
Open cmd in the same directory of the project; Use following command:
pyinstaller --noconfirm --onedir --windowed --icon=XYZ.ico "C:/Users/.../Tkinter_Project/Main.py"
Application was created with no dependencies. If there is any config or image files used, simply copy paste it to the 'dist' folder which gets created.

Sikulix install - jython

I need to test unity 3d app on mobile device (or emulator). I do not want to press on buttons using coordinates only, so I want to use Sikuli. I will describe steps that I have already done:
Downloaded and installed Sikuli v1.1.1
Installed java 32bit
Installed jython 2.7.0
jython added as interpreter in my project in pyCharm
sikulixapi is run
Copied Sikuli lib from appData to jython folder
Created Python file and added imports
And when I try to run project I receive this error:
D:\jython2.7.0\bin\jython.exe -Dpython.path=D:\python_projects\sikuli_test D:/python_projects/sikuli_test/test_sikuli.py
Tra**ceback (most recent call last):
File "D:/python_projects/sikuli_test/test_sikuli.py", line 1, in <module>
from sikuli import Region
File "D:\jython2.7.0\Lib\sikuli\__init__.py", line 5, in <module>
from org.sikuli.basics import Debug
ImportError: No module named sikuli
Process finished with exit code -1**
What am I doing wrong?
The following works for me:
import org.sikuli.script.SikulixForJython
before
from sikuli import *
In your jython project interpreter, set the environment variables path to where your sikuli jar is placed. That should fix it.
Also make sure to use this in your code
import org.sikuli.script.SikulixForJython
from sikuli import *

Py2exe compiles properly but the built application doesn't work

I am using Python 2.7 to build my application. Within it, I used few packages which are numpy, scipy, csv, sys, xlwt, time, wxpython and operator.
All the above packages are in 64-bit, and I am using python 2.7(64-bit version) in Aptana Studio 3(64-bit version) in Windows 7 Professional (64-bit version).
At last, I'd like to compile my project to an application using following code, the file name is py2exeTest.py:
from distutils.core import setup
import numpy # numpy is imported to deal with missing .dll file
import py2exe
setup(console=["Graphical_Interface.py"])
Then in cmd, I switched to the directory of the project and used following line to compile it:
python py2exeTest.py py2exe
Everything goes well, it generates an application under dist directory, and the application name is Graphical_Interface.exe.
I double clicked it, but there is a cmd window appears, and a python output windows flashes, then both of them disappeared. I tried to run the application as an administrator, the same outcome I've had.
May I know how to work this out?
Thanks!
EDIT:
I've managed to catch the error information that flashes on the screen. The error info I had is:
Traceback (most recent call last):
File "Graphical_Interface.py", line 397, in <module>
File "Graphical_Interface.py", line 136, in __init__
File "wx\_core.pyc", line 3369, in ConvertToBitmap
wx._core.PyAssertionError: C++ assertion "image.Ok()" failed at ..\..\src\msw\bitmap.cpp(802) in wxBitmap::CreateFromImage(): invalid image
I used one PNG image in the project, the code is like follows:
self.workflow = wx.Image("Work Flow.png", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
wx.StaticBitmap(self.panel_settings, -1, self.workflow, (330,270), (self.workflow.GetWidth(), self.workflow.GetHeight()))
I tried to comment the above chunk out from the project, and the application works properly. However, I need the image to show up in the application.
May I know how to deal with it?
Thanks.
Hiding console window of Python GUI app with py2exe
When compiling graphical applications you can not create them as a console application because reasons (honestly can't explain the specifics out of my head), but try this:
from distutils.core import setup
import numpy
import py2exe
import wxpython
setup(window=['Graphical_Interface.py'],
options={"py2exe" { 'boundle_files' : 1}})
Also consider changing to:
http://cx-freeze.sourceforge.net/
It works with Python3 and supports multiple platforms.
A cx_freeze script would look something like:
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
base = "Win32GUI"
includefiles = ['/folder/image.png']
setup( name = "GUIprog",
version = "0.1",
description = "My GUI application!",
options = {"build_exe": build_exe_options, 'include_files' : includefiles},
executables = [Executable("Graphical_Interface.py", base=base)])
No worries, I've got the solution.
It turns out that the image is in the project folder rather than in the dist folder. So I have two solutions:
Copy the image into dist folder
Include the full path of the image in the code.
Thanks for your help.

Missing dll's when using Py2Exe, PyGtk, and Glade

I'm trying to build my first application using Py2Exe and I'm following the instructions listed at http://www.py2exe.org/index.cgi/Py2exeAndPyGTK, with the exception that I'm creating the UI with Glade. (Its just a very minimal gui with a close button and a label.)
When I try to run the resulting exe, I get an error dialog asking me to check the log file which contains the following error message:
Traceback (most recent call last):
File "pygtkpy2exetest.py", line 8, in <module>
File "gtk\glade.pyc", line 12, in <module>
File "gtk\glade.pyc", line 10, in __load
ImportError: DLL load failed: The specified module could not be found.
So I started digging around a bit, the first thing that I noticed is that it looks like py2exe is not able to find glib modules:
The following modules appear to be missing
['gdk', 'unix', 'glib.GError', 'glib.IOChannel', 'glib.IO_ERR', 'glib.IO_FLAG_APPEND', 'glib.IO_FLAG_GET_MASK', 'glib.IO_FLAG_IS_READABLE', 'glib.IO_FLAG_IS_SEEKABLE', 'glib.IO_FLAG_IS_WRITEABLE', 'glib.IO_FLAG_MASK', 'glib.IO_FLAG_NONBLOCK', 'glib.IO_FLAG_SET_MASK', 'glib.IO_HUP', 'glib.IO_IN', 'glib.IO_NVAL', 'glib.IO_OUT', 'glib.IO_PRI', 'glib.IO_STATUS_AGAIN', 'glib.IO_STATUS_EOF', 'glib.IO_STATUS_ERROR', 'glib.IO_STATUS_NORMAL', 'glib.Idle', 'glib.MainContext', 'glib.MainLoop', 'glib.OPTION_ERROR', 'glib.OPTION_ERROR_BAD_VALUE', 'glib.OPTION_ERROR_FAILED', 'glib.OPTION_ERROR_UNKNOWN_OPTION', 'glib.OPTION_FLAG_FILENAME', 'glib.OPTION_FLAG_HIDDEN', 'glib.OPTION_FLAG_IN_MAIN', 'glib.OPTION_FLAG_NOALIAS', 'glib.OPTION_FLAG_NO_ARG', 'glib.OPTION_FLAG_OPTIONAL_ARG', 'glib.OPTION_FLAG_REVERSE', 'glib.OPTION_REMAINING', 'glib.OptionContext', 'glib.OptionGroup', 'glib.PRIORITY_DEFAULT', 'glib.PRIORITY_DEFAULT_IDLE', 'glib.PRIORITY_HIGH', 'glib.PRIORITY_HIGH_IDLE', 'glib.PRIORITY_LOW', 'glib.Pid', 'glib.PollFD', 'glib.SPAWN_CHILD_INHERITS_STDIN', 'glib.SPAWN_DO_NOT_REAP_CHILD', 'glib.SPAWN_FILE_AND_ARGV_ZERO', 'glib.SPAWN_LEAVE_DESCRIPTORS_OPEN', 'glib.SPAWN_SEARCH_PATH', 'glib.SPAWN_STDERR_TO_DEV_NULL', 'glib.SPAWN_STDOUT_TO_DEV_NULL', 'glib.Source', 'glib.Timeout', 'glib.child_watch_add', 'glib.filename_display_basename', 'glib.filename_display_name', 'glib.filename_from_utf8', 'glib.get_application_name', 'glib.get_current_time', 'glib.get_prgname', 'glib.glib_version', 'glib.idle_add', 'glib.io_add_watch', 'glib.main_context_default', 'glib.main_depth', 'glib.markup_escape_text', 'glib.set_application_name', 'glib.set_prgname', 'glib.source_remove', 'glib.spawn_async', 'glib.timeout_add', 'glib.timeout_add_seconds', 'glib.uri_list_extract_uris']
If I try to manually add the glib module to the "includes" option in setyp.py it doesn't have any effect.
Digging further, I opened up the resulting exe with Dependency Walker, it shows me that there are two dll's missing, MPR.DLL and SHLWAPI.DLL, but both of these are in my system32 folder.
If I just create the UI without glade, I have no problems what so ever, but given that I've already created another application with glade, I'd like to stick with glade if possible. Any suggestions on where to go from here? I'm using Python 2.7, Py2Exe 0.6.9, PyGtk 2.22.6 on Windows XP.
Thanks,
Brent
i'm also having the same problem, however after spending few hours in google, i manage to find the solution.
use dependencywalker and profiling ur xxx.exe
u can download from http://www.dependencywalker.com/
find out the error file (for my case, they are MSJAVA.DLL and libxml2-2.dll)
search ur pc directory and copy them into 'dist' folder. u also can download the missing DLL from other website.
run the dependencywalker again, u will see the error reduce or become no more.
xxx.exe is executable now.
I hope above would helps those who are facing such kind of problem.
Neoh !!!share is care!!!
This may not help, but I had the same issue & it turned out to be a gtk installation issue. This caused problems with the gtk._gtk.pyd file which py2exe produced.
The setup.py I used was pretty much the same as that which py2exe suggests on http://www.py2exe.org/index.cgi/Py2exeAndPyGTK
I had the same error once and fix it. when you run the setup.py py2exe your files are created in win32. pyo as glade.pyo, and when you do you believe in win64 .pyc, at least that's what I experience, which ran the py2exe my app in window7-64 and then gave me the error, but when you create the executable on win32 I did, I think the glade.pyo, then gave me the same error, go to my project and do everithing with
gtk.Builder()
` not use more
gtk.glade.XML("file.glade")
and remove the import gtk.glade of my .py files and my setup.py just use
import gtk
and everything worked perfectly.
I hope my experiences will be of your help.

Categories