pythonmacospython-2.7tkinteractivetcl

Getting error using Tkinter in python on mac OS X


I tried to run a nltk code for drawing parse trees. I got the error that tkinter module is not installed.

These are the error messages I got:

1. UserWarning: nltk.draw package not loaded (please install Tkinter library).
   warnings.warn("nltk.draw package not loaded")

2. import _tkinter # If this fails your Python may not be configured for Tk
   ImportError: No module named _tkinter

After some searches I installed the ActiveTcl 8.5.18.0 using this instructions.

But when I try to run my code I still get the sam error. I tried

import Tkinter

but I get the second error message above again.

File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter

I also looked at Tkinter documentations and it is mentioned that the correct installation of Tkinter can be verified by running the following command which again gives me another error.

command: python -m tkinter
error: /usr/local/opt/python/bin/python2.7: No module named tkinter

I found this answers on Stackoverflow for my problem but they are either not very clear or not applicable to my case.

  1. How to get tkinter working with Ubuntu's default Python 2.7 install? Problems: tk-dev is not available for OS X (it is same as ActiveTcl) and I couldn't figure out how to rebuilt my python using make

  2. Tkinter: "Python may not be configured for Tk" Problems: very vague. I don't know what should I do

please help.


Solution

  • You should install ActivePython rather than ActiveTcl, and use it as your preferred Python.

    The problem is your Python install isn't picking up your Tcl install, and the simplest way to solve that is to install a Python version that is configured for Tk, which ActivePython is: http://www.activestate.com/activepython

    The issue is that the _tkinter Python module is not installed in your build, which is a required bridge between Python and Tk. You will have to reinstall nltk and any other packages you are using, unfortunately, as the versions you have will be installed for your current Python and not your new one.