0

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.

bit_scientist
  • 1,496
  • 2
  • 15
  • 34

1 Answers1

1

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
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • thank you, I updated six and cx_freeze as you mentioned earlier. And looked up the link you provided and didn't clearly understand what I should do, to be honest. I am using `matplotlib`to open and save a `png` format, so I added `import matplotlib matplotlib.use('Agg')` and deleted `import matplotlib.pyplot as plt`, but it didn't pop up a window showing up the graph I used to see. Please correct me – bit_scientist Aug 02 '18 at 04:24
  • @voo_doo I don't know how to explain this any more clearly, but I'll try one more time: You can only use a non-interactive backend if you don't want to display any graphs. If you do want to display graphs, you have to pick one of the graphical backends and bundle it with your app. – abarnert Aug 02 '18 at 04:32
  • thanks, I missed the _non-graphically_ part. Sorry. Let me try – bit_scientist Aug 02 '18 at 04:34
  • for anyone who has similar issues related to cx_freeze, I'd recommend **pyinstaller** as described [here](https://medium.com/dreamcatcher-its-blog/making-an-stand-alone-executable-from-a-python-script-using-pyinstaller-d1df9170e263) – bit_scientist Aug 02 '18 at 07:59
  • @voo_doo Each tool has its own set of annoyances… but yeah, in my experience, `cx_Freeze` is the worst of the big four at picking up indirect dependencies like this. `pyInstaller` has a bunch of special cases built in for the most common annoyances; `py2exe` and `py2app` have that plus some complicated guessing code; `cx_Freeze` has an out-of-date wiki that's gradually being replaced by [samples in the GitHub repo](https://github.com/anthony-tuininga/cx_Freeze/tree/master/cx_Freeze/samples/matplotlib) and expects you to figure it out youself. – abarnert Aug 02 '18 at 08:26
  • 1
    @voo_doo But, on the other hand, `cx_Freeze` is usually the best for cross-platform support (as in: once things work, they usually the same on at least the big three platforms) and staying up to date with Python changes. – abarnert Aug 02 '18 at 08:27
  • you are right, I just needed to compile it as exe and pyinstaller was enough. – bit_scientist Aug 02 '18 at 09:12