cx_freeze with Tkinter & matplotlib backend, No module named FileDialog

5.8k views Asked by At

Using cx_Freeze to build simple matplotlib applications work great, however i'm running into a problem when attempting to create a standalone executable from a Tkinter & Matplotlib app.

Here's a minimal example which would reproduce the error:

#!/usr/bin/env python

import matplotlib
matplotlib.use('TkAgg')

from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
# implement the default mpl key bindings
from matplotlib.backend_bases import key_press_handler


from matplotlib.figure import Figure

import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk

root = Tk.Tk()
root.wm_title("Embedding in TK")


f = Figure(figsize=(5, 4), dpi=100)
a = f.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)

a.plot(t, s)


# a tk.DrawingArea
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

toolbar = NavigationToolbar2TkAgg(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)


def on_key_event(event):
    print('you pressed %s' % event.key)
    key_press_handler(event, canvas, toolbar)

canvas.mpl_connect('key_press_event', on_key_event)


def _quit():
    root.quit()     # stops mainloop
    root.destroy()  # this is necessary on Windows to prevent
                    # Fatal Python Error: PyEval_RestoreThread: NULL tstate

button = Tk.Button(master=root, text='Quit', command=_quit)
button.pack(side=Tk.BOTTOM)

Tk.mainloop()
# If you put root.destroy() here, it will cause an error if
# the window is closed with the window manager.

Specifically from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg seems to be causing the problem. If I run my own app, I would get the same error as if I ran this one above:

Traceback (most recent call last):
  File "C:\Anaconda2\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
    exec(code, m.__dict__)
  File "Calipso.py", line 25, in <module>
  File "C:___.py", line 14, in <module>
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
  File "C:\Users\Grant\Documents\GitHub\vocal\calipso\build\exe.win32-2.7\matplotlib\backends\backend_tkagg.py", line 7, in <module>
    from six.moves import tkinter_filedialog as FileDialog
  File "C:\Anaconda2\lib\site-packages\six.py", line 203, in load_module
    mod = mod._resolve()
  File "C:\Anaconda2\lib\site-packages\six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "C:\Anaconda2\lib\site-packages\six.py", line 82, in _import_module
    __import__(name)
ImportError: No module named FileDialog

My setup.py looks like:

import os
import sys
from distutils.core import setup
import cx_Freeze
import matplotlib

base = "Console"

executable = [
    cx_Freeze.Executable("Calipso.py", base = base)
]

build_exe_options = {"includes":["matplotlib.backends.backend_tkagg", "ccplot.algorithms",
                                 "ccplot.hdf", "Tkinter", "tkFileDialog"],
                     "include_files":[(matplotlib.get_data_path(), "mpl-data")],
                     "excludes": ["collections.abc"],
                     }

cx_Freeze.setup(
    name = "py",
    options = {"build_exe": build_exe_options},
    version = "0.0",
    description = "standalone",
    executables = executable
)

How can I make sure to bundle FileDialog?

1

There are 1 answers

2
Syntactic Fructose On BEST ANSWER

Found a fix to this problem. The issue is that FileDialog is a separate package from Tkinter all together, so my script now looks like:

import os
import sys
from distutils.core import setup
import cx_Freeze
import matplotlib

base = "Console"

executable = [
    cx_Freeze.Executable("Calipso.py", base = base)
]

build_exe_options = {"includes":["matplotlib.backends.backend_tkagg", "ccplot.algorithms",
                                 "ccplot.hdf"],
                     "packages": ["Tkinter", "tkFileDialog"],
                     "include_files":[(matplotlib.get_data_path(), "mpl-data")],
                     "excludes": ["collections.abc"],
                     }

cx_Freeze.setup(
    name = "py",
    options = {"build_exe": build_exe_options},
    version = "0.0",
    description = "standalone",
    executables = executable
)

Edit: Fixed colon outside quotes.