2

This question already has an answer here:

I'm decently new to using a Raspberry Pi and I'm trying to set up a simple GUI application to launch from my Pi. I'm using a Raspberry Pi3, with Python 3.4.3 installed.

When I type python:

~$ python
Python 3.4.3 |Continuum Analytics, Inc.| (default, Aug 21 2015, 00:53:08)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.

When I try to import tkinter:

>>> import tkinter
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/home/pi/miniconda3/lib/python3.4/tkinter/__init__.py", line 38, in <module>
        import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named '_tkinter'
>>>

So, I can see that the error is coming from miniconda3. The only reason I had it installed was to use PyQt5, but I was getting a similar error when I tried to use that, so I switched over to Tkinter because there were more examples of using it with a pi online.

I have run the following commands to try to install Tkinter:

sudo apt-get install tk-dev
sudo apt-get install tk8.6-dev
sudo apt-get install python-imaging-tk
sudo apt-get install python-tk
sudo apt-get install python3-tk

How can I fix this? I have read in some places to make python again, but no one says how to do that.

Thanks guys!

marked as duplicate by karel, Videonauth, dessert, Eliah Kagan, Eric Carvalho Dec 7 '17 at 17:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • It seems you are launching miniconda python not python with comes with raspberry-pi distribution. Could post the output of which python and try if tk works when lauching python using /usr/bin/python – user.dz Mar 25 '17 at 18:17
  • 1
    @user.dz This was exactly the issue! Now there is no import issue when I try to import. Thank you so much! – jschach Mar 25 '17 at 23:56
2

Install the Tkinter package for writing Tk applications with Python 3.x. In all currently supported versions of Ubuntu, open the terminal and type:

sudo apt install python3-tk # for Python 2.x install python-tk 

Then run /usr/bin/python3 to start the default Python 3 that comes with Ubuntu and import tkinter will work.

1
  • It seems that you are launching mini-conda python not python that comes with raspberry-pi distribution. You could check the output of:

    which python
    

    Expected output: /usr/local/bin/python or mini-conda custom installation path (if it was added before.)

  • Test also if Tk works when launching python using absolute path:

    /usr/bin/python
    
  • To complete my answer, if you like to use miniconda python, use its own installation method:

    conda install packagename
    

    Reference: Miniconda homepage, :) I'm not aware of the exact package name of tkinter.

Not the answer you're looking for? Browse other questions tagged or ask your own question.