15

I got problems with importing tkinter after installing Python version 3.4.2 with pyenv. My system Python is version 2.7.6. I am using Ubuntu 14.04. For the following sample script t.py:

import _tkinter

print ("Hello")

I get:

$ pyenv global system
$ python --version
Python 2.7.6
$ python t.py
Hello
$ pyenv global 3.4.2
$ python --version
Python 3.4.2
$ python t.py
Traceback (most recent call last):
  File "t.py", line 3, in <module>
    import _tkinter
ImportError: No module named '_tkinter'
    Traceback (most recent call last):
      File "t.py", line 3, in <module>
        import _tkinter
    ImportError: No module named '_tkinter'

Note that pyenv installed Python version 3.4.2 in ~/.pyenv/versions/3.4.2/.

7
  • Don't import private modules unless you know what you're doing.
    – simonzack
    Commented Oct 14, 2014 at 9:57
  • @simonzack Is tkinter a private module? Commented Oct 14, 2014 at 10:00
  • In this case yes, names prefixed with _ mean private as a convention.
    – simonzack
    Commented Oct 14, 2014 at 10:01
  • 1
    I just followed the instructions at wiki.python.org/moin/TkInter .. It says you should first try to import _tkinter.. and importing tkinter without underscore does not work either.. Commented Oct 14, 2014 at 10:02
  • That's for testing the install.
    – simonzack
    Commented Oct 14, 2014 at 10:03

3 Answers 3

35

This problem seems to be solved now using the approach described in this post:

  • First uninstall Python 3.4.2 : pyenv uninstall 3.4.2, then
  • Run sudo apt-get install tk-dev
  • And reinstall Python 3.4.2 : pyenv install 3.4.2
4
  • 2
    In Centos, you have to install tkinter using this command: sudo yum install python34-tk* Commented Mar 22, 2016 at 14:33
  • 1
    Awesome! No need to set env variables!
    – Fábio
    Commented May 19, 2021 at 13:47
  • 1
    This worked for me although I don't understand why I have to uninstall python and then install tk and then reinstall Commented Dec 23, 2021 at 10:05
  • on Arch, this procedure with sudo pacman -S tk did the trick for me (Python 3.9.12 via pyenv). Commented Apr 24, 2022 at 14:29
1

For MacOS you can try installing tcl-tk via homebrew and then activating the env. vars. mentioned in its caveats section, as detailed in this answer.

0

Change your code to:

import tkinter

Documentation link:

Most of the time, tkinter is all you really need, but a number of additional modules are available as well. The Tk interface is located in a binary module named _tkinter. This module contains the low-level interface to Tk, and should never be used directly by application programmers. It is usually a shared library (or DLL), but might in some cases be statically linked with the Python interpreter.

3
  • It still does not work. See step 1 at wiki.python.org/moin/TkInter . It says you should first try to import _tkinter.. Commented Oct 14, 2014 at 9:57
  • @HåkonHægland That's for testing the install.
    – simonzack
    Commented Oct 14, 2014 at 10:02
  • @simonzack Yes that was exactly what I was trying to do.. testing the install.. I never use import _tkinter in my regular code.. Commented Oct 14, 2014 at 10:26

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