5

I've built a Python (2.7) app that uses Tkinter and am trying to build a Windows7 .exe using Pyinstaller (3.2). The app works find in windows is I run it as python myapp.py, but once compiled into a pyinstaller distributable, I get this error message:

ImportError: No module named Tkinter

Just to be sure, the top of myapp.py contains:

from copy import deepcopy
import cPickle as pickle
import Tkinter as tk
from PIL import ImageTk

Checking the distribution directory, I see tk85.dll, tcl85.dll and two directories that see pertinent, tcl/ and tk/

I've found many references to secondary Tkinter dependencies, such as matplotlib which imports Tkinter itslef, but I've not found any details of a direct dependency like this.

Any ideas how to get this one working?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
KirkD-CO
  • 1,603
  • 1
  • 22
  • 35
  • I converted a Python Script to exe but found PyInstaller creating problems. Try doing it using Cx_Freeze instead. It is hassle-free and works like a charm. – AR06 May 10 '16 at 07:32
  • Don't know that one. I'll give it a try and report back once I do. – KirkD-CO May 11 '16 at 02:35
  • I gave cx_Freeze a try and I'm much closer. I had to throw in a couple of excludes to avoid certain package errors. Now, however, I'm stuck with a font problem. One of the packages I'm using also uses Pillow and some PIL fonts. I've found where the fonts are stored for the package, but can't quite get things set up to know they are there. I copied them directly to the build/exe directory, but no luck. I also see the section in the cx_Freeze docs about including data files, but I'm not sure how to make that work. – KirkD-CO May 11 '16 at 13:21

3 Answers3

2

Check https://github.com/pyinstaller/pyinstaller/issues/1584. There is an issue with the PIL hook, which excludes the tkinter module.

One solution is to modify the hook file hook-PIL.py located in YourPythonFolder\Lib\site-packages\PyInstaller\hooks by removing the modname_tkinter from excludedimports.

Or just change the order of the import statements in your code. Do:

from PIL import ImageTk
import Tkinter as tk
Repiklis
  • 399
  • 6
  • 20
  • Nice solution. Mind that changing the order of import does not work using `from Tkinter import *`, but this can be resolved easily as done above. – tfv Nov 03 '16 at 05:10
0

Have you checked: https://github.com/pyinstaller/pyinstaller/issues/1877 (or other issues)? https://github.com/pyinstaller/pyinstaller/wiki/If-Things-Go-Wrong

quote from issue 1877 "It looks like the hook-_tkinter.py is not able to handle custom compiled Tk." Possible workaround: "Thanks, after installed tkinter, tix, tcl-devel and tk-devel using yum installation, It's now work fine. "

Otherwise, Py2exe is also an option for creating a .exe file, and i have used it plenty of times with tkinter with no issues.

glls
  • 2,325
  • 1
  • 22
  • 39
  • I haven't seen that. I'm not using a custom build, but I will try to install the additional packages. I'll give it a try and report back here – KirkD-CO May 11 '16 at 11:23
  • Unfortunately, I've found that I already have all of those packages installed. – KirkD-CO May 11 '16 at 12:31
  • I also tried py2exe, and found I had to add some DLL excludes (numpy-atlas.dll and MSVCP90.dll). Once compiled, it fails with a number of log entries saying "ImportError: numpy.core.multiarray failed to import" I've searched for solutions to that but found no solution. – KirkD-CO May 11 '16 at 12:40
0

I had an extension to this problem. Including Tkinter in the list of hiddenimports enabled me to display plots but I could not save them. By adding FileDialog, tkFileDialog and tkMessageBox into hidden imports in my spec file solved the problem. That is, hiddenimports=['FileDialog', 'Tkinter', 'tkFileDialog', 'tkMessageBox', ]

Angus

AnGus King
  • 21
  • 2