Search code examples
pythontkinterbuildsite-packages

build is successful but code doesn't work, throwing ImportError


My issue is little different from existing ones on the WEB. Let me try highlighting it in detail. I have this code to do some plotting by reading a .txt file specified. I was going to build it as usually as I did before. My setup.py is supposed to do the job. The content of setup.py is here

As you can observe I have no any package related to tkinter in both my above files. I did python setup.py build in terminal and it executed well. But can't use my actual code with windows command script

build\\exe.win-amd64-3.5\\draw_precision_recall_curve.exe D:\dataset\Fire_Smoke\Train\filelist.txt 
 pause 

It throws this trace:

Traceback (most recent call last):
  File "C:\python354\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
    module.run()
  File "C:\python354\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
    exec(code, m.__dict__)
  File "draw_precision_recall_curve.py", line 3, in <module>
    import matplotlib.pyplot as plt
  File "C:\python354\lib\site-packages\matplotlib\pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "C:\python354\lib\site-packages\matplotlib\backends\__init__.py", line 62, in pylab_setup
    [backend_name], 0)
  File "C:\python354\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 4, in <module>
    from . import tkagg  # Paint image to Tk photo blitter extension.
  File "C:\python354\lib\site-packages\matplotlib\backends\tkagg.py", line 5, in <module>
    from six.moves import tkinter as Tk
  File "C:\python354\lib\site-packages\six.py", line 92, in __get__
    result = self._resolve()
  File "C:\python354\lib\site-packages\six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "C:\python354\lib\site-packages\six.py", line 82, in _import_module
    __import__(name)
ImportError: No module named 'tkinter'

I was not able to solve this looking at the related posts here and there. Any help is appreciated.


Solution

  • You may not be using tkinter, but you are using matplotlib, and it uses tkinter.

    In fact, matplotlib gives you a choice of backends. But IIRC, the default is tkinter (or maybe TkAgg, which uses tkinter) on Windows if you haven't installed the optional Win32 native backend installed, and always on non-Mac Unix.

    So:

    • If you're only using matplotlib non-graphically—e.g., to generate files to save—specify a non-interactive backend explicitly.
    • If you're using it to display graphs, you either need to bundle tkinter in your app, or pick a different interactive backend and bundle that in your app.

    For example, let's say you just want to generate a bunch of graphs as PNG files. Instead of doing that through the default TkAgg backend, you can use the AGG non-interactive backend, like this:

    import matplotlib
    matplotlib.use('AGG')
    import matplotlib.pyplot as plt