4

I have a small script in python2.7 that I want to convert into Windows executable. I use pyinstaller for this.

The script:

import sys 
import matplotlib.pyplot as plt
import matplotlib.image as mpimg


def get_inputs():
    coor = raw_input(">>>top x left: ").replace(" ", "")
    top, left = coor.split("x")
    top = int(top.strip())
    left = int(left.strip())
    return top, left 

def plot_location(top, left):
    img= mpimg.imread('nbahalfcourt.jpg')
    plt.imshow(img)
    plt.scatter(left, top)
    plt.grid()
    plt.show()

def main():
    top, left = get_inputs()
    plot_location(top, left)

if __name__ == '__main__':

    print "Input top x left coordinates (no space) eg: 44x232"

    run = True 
    while run:
        main()

Basically, the script just plots a point on a grid.

The converting process finishes successfully. When I run the .exe however I've got the ImportError (see below) even though I have no reference to Tkinter anywhere.

enter image description here

What could went wrong here?

nutship
  • 4,624
  • 13
  • 47
  • 64
  • 1
    I have a feeling that `matplotlib` uses the `Tkinter` module internally, but imports it in a non-standard way. Then `pyinstaller` doesn't notice Tkinter is needed, and subsequently doesn't bundle it into the executable. I wonder if explicitly putting `import Tkinter` at the top of your script would help? – Kevin Oct 07 '14 at 17:32
  • That did the trick! Thanks buddy. Post it as an answer if you wish, so I can accept for the record. – nutship Oct 07 '14 at 17:49
  • 1
    explicitly importing them so `pyinstaller` can definitely see them is a perfectly good solution, there's also command line flag for explicitly telling `pyinstaller` to include extra modules `--hidden-import=Tkinter` – GP89 Oct 07 '14 at 18:02

2 Answers2

5

I have a feeling that matplotlib uses the Tkinter module internally, but imports it in a non-standard way. Then pyinstaller doesn't notice Tkinter is needed, and subsequently doesn't bundle it into the executable.

Try explicitly putting import Tkinter at the top of your script.

Kevin
  • 74,910
  • 12
  • 133
  • 166
3

I had the same issue and none of the solutions here worked. I was using Pyinstaller 3.2 (the latest version at the time) and everything was fixed (and no import statements needed) when I upgraded to the latest developer version using

pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip

This seems to indicate that at the times of writing this that all the kinks to this issue are still being worked out

EDIT: As of January 15 2017 Pyinstaller version 3.2.1 was released. I now use this and it solves this issue along with others like this and this that I could previously only solve by using the developer version. So I highly recommend upgrading to the latest version if you haven't already.

Community
  • 1
  • 1
Frikster
  • 2,755
  • 5
  • 37
  • 71
  • Yep, this worked for me. Also using 3.2 and getting import error for Tkinter. Installing the latest developer version resolved it thanks. – David Clarke Sep 08 '16 at 22:35
  • Also for me using the 3.3 dev version was the only way to fix the issue, thanks – dipanda Nov 07 '16 at 09:02