PyInstaller: “No module named Tkinter”

隐身守侯 提交于 2020-02-02 03:19:06

问题


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?


回答1:


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



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/37105350/pyinstaller-no-module-named-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!