25

I am using Ubuntu 13.10 and Python 3.3.4 using pyenv. I have no problem using other modules. When I enter python console and write

import tkinter

It outputs

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

I already installed python3-tk and tk-devel.

5
  • Does /usr/bin/python3 -c'import tkinter' succeed? Commented Mar 21, 2014 at 9:43
  • Yes it worked but pyenv installs multiple version. You can change active python version with single command. It installs under ~/.pyenv/versions. ~/.pyenv/versions/3.3.4/bin/python3 -c'import tkinter' didn't worked. Commented Mar 21, 2014 at 21:03
  • 1
    if it works with /usr/bin/python3 then try to recreate the environment Commented Mar 22, 2014 at 2:27
  • @J.F.Sebastian JFI: pyenv works different from regular virtualenvs. Commented Jul 8, 2015 at 16:51
  • if you are using pyenv for Python environments management and want IDLE work there is step by step guide stackoverflow.com/questions/61870688/… Commented May 18, 2020 at 22:06

3 Answers 3

40

You need to have the Tk development files/headers available during pyenv install.

On Ubuntu (15.04) the following should provide them: sudo apt-get install tk-dev.

After this, pyenv install 3.4.3 (or pyenv install 2.7.10 etc) should pick it up and have support for Tk.

(Reference issue in pyenv)

Sign up to request clarification or add additional context in comments.

5 Comments

this should probably go in the pyenv faq
I just added it to the requirements in common-build-problems wiki page
For Red Hat / Amazon Linux, this is: sudo yum install tkinter
I'm having the same issue on MacOS. Anyone have ideas?
This was my issue. I was still trying to get it to work without creating a new env
18

This is a guide for Mac users,

  1. Uninstall your python version. For example: pyenv uninstall 3.7.2
  2. Install TK: brew install tcl-tk
  3. Follow the post install steps and config tk:
export LDFLAGS="-L/usr/local/opt/tcl-tk/lib"
export CPPFLAGS="-I/usr/local/opt/tcl-tk/include"
export PATH=$PATH:/usr/local/opt/tcl-tk/bin
  1. Reload your terminal session and make sure all settings above are correctly set
  2. Install python: pyenv install 3.7.2

The TK should work now

3 Comments

thats correct. I've just posted the step by step guide for ones who may need it -stackoverflow.com/questions/61870688/…
ACTUALLY What solved my problem is: brew install python-tk and not the tcl... thing.
thanks @JohnnyJS, your solution works for me. btw i'm on python-3.9
4

For Mac users,

  1. Uninstall pyenv completely: brew uninstall pyenv && rm -rf ~/.pyenv.
  2. install zlib, tcl-tk and pyenv
brew update
brew install zlib
brew install tcl-tk #otherwise we may have problems with tf package
brew install pyenv
  1. Make compilers find zlib. Add this to your ~/.bashrc or ~/.zshrc (if you youse oh-my-zsh)
# For compilers to find zlib you may need to set:
export LDFLAGS="${LDFLAGS} -L/usr/local/opt/zlib/lib"
export CPPFLAGS="${CPPFLAGS} -I/usr/local/opt/zlib/include"

# For pkg-config to find zlib you may need to set:
export PKG_CONFIG_PATH="${PKG_CONFIG_PATH} /usr/local/opt/zlib/lib/pkgconfig"

Then you are set up!. For more details check this github gist.

1 Comment

If you just want to run a tool that requires tk, you can use pipx to create a venv and script that goes into $PATH to launch it without interfering with PyEnv etc. E.g. to run the Thonny GUI using Homebrew's Python + TK libraries: brew install python python-tk && cd /opt/homebrew/Cellar/[email protected]/3.11.6_1/bin && ./python3 -m pip install pipx && ./python3 -m pipx install thonny && cd && .local/bin/thonny

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.