5

I recently met a problem while installing the newest python3.X.
Installed it using the Python-3.4.2.tar.xz package from python.org After, the intallation I tried importing the tkinter module but didn't succeed.

The output of import tkinter was:

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

I also tried following solutions:

but none of them helped.
While trying these solutions, if noticed that the error says:

import _tkinter # If this fails your Python may not be configured for Tk

then i googled about it and found this.
Reading the Checking your Tkinter support section, the Step 1 failed and was stuck in this line

If you install Tcl/Tk in the default locations, simply rerunning "make" should build the _tkinter extension.

Regarding above line, my question is:
Where to find a make file to run a make command on ?

And, how do I configure the tkinter so that Python3.4.2 accepts it ?


EDIT:

I forgot to mention but import tkinter do works for the default installation (Python-3.4.0) of the Python in Ubuntu 14.04.1

3
  • 1
    Do you really need 3.4.2? I'm asking because tk will still be 8.6 on 14.04 Oct 30, 2014 at 8:26
  • @SylvainPineau yes, as the Python Official Site quotes "Python 3.4 includes a range of improvements of the 3.x series, including hundreds of small improvements and bug fixes."
    – devGeek
    Oct 30, 2014 at 8:37
  • I've updated my answer to create the tkinter module for your 3.4.2 version built from source. Oct 30, 2014 at 10:50

4 Answers 4

9

In order to build python3.4.2 from source with the _tkinter module you need to install the following build dependency:

sudo apt-get install tk8.6-dev

Then all you have to do is running make again to add _tkinter support as the setup.py file will automatically detect the tk/tcl headers and create the module:

~/Downloads/Python-3.4.2$ make
running build
running build_ext
building '_tkinter' extension
gcc -pthread -fPIC -Wno-unused-result -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Werror=declaration-after-statement -DWITH_APPINIT=1 -I/usr/include/tcl8.6 -I/usr/X11/include -I./Include -I. -IInclude -I/usr/include/x86_64-linux-gnu -I/usr/local/include -I/home/sylvain/Downloads/Python-3.4.2/Include -I/home/sylvain/Downloads/Python-3.4.2 -c /home/sylvain/Downloads/Python-3.4.2/Modules/_tkinter.c -o build/temp.linux-x86_64-3.4/home/sylvain/Downloads/Python-3.4.2/Modules/_tkinter.o
gcc -pthread -fPIC -Wno-unused-result -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Werror=declaration-after-statement -DWITH_APPINIT=1 -I/usr/include/tcl8.6 -I/usr/X11/include -I./Include -I. -IInclude -I/usr/include/x86_64-linux-gnu -I/usr/local/include -I/home/sylvain/Downloads/Python-3.4.2/Include -I/home/sylvain/Downloads/Python-3.4.2 -c /home/sylvain/Downloads/Python-3.4.2/Modules/tkappinit.c -o build/temp.linux-x86_64-3.4/home/sylvain/Downloads/Python-3.4.2/Modules/tkappinit.o
gcc -pthread -shared build/temp.linux-x86_64-3.4/home/sylvain/Downloads/Python-3.4.2/Modules/_tkinter.o build/temp.linux-x86_64-3.4/home/sylvain/Downloads/Python-3.4.2/Modules/tkappinit.o -L/usr/X11/lib -L/usr/lib/x86_64-linux-gnu -L/usr/local/lib -ltk8.6 -ltcl8.6 -lX11 -o build/lib.linux-x86_64-3.4/_tkinter.cpython-34m.so

Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2                  _dbm                  _gdbm              
_lzma                 _sqlite3                                 
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
[...]

Now you can import tkinter in python3.4.2:

~/Downloads/Python-3.4.2$ ./python 
Python 3.4.2 (default, Oct 30 2014, 11:34:17) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> 

Original answer:

Unless you really need python3.4.2, I would just use the default python3 version on 14.04 (3.4.0)

Then all you have to do is installaling the following packages:

sudo apt-get install python3-tk tk

And start the python interpreter this way:

/usr/bin/python3

Otherwise you'll always get the version that you installed in /usr/local (3.4.2).

Importing tk in python3 should work now:

$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> 
7
  • Problem still persists. tk8.6-dev dependency is already installed. And that makes a point, if tk8.6-dev wouldn't have been previously installed tkinter module wouldn't have worked for Python-3.4.0 either.
    – devGeek
    Oct 30, 2014 at 14:24
  • @devGeek Actually you don't need it for 3.4.0. it's only required to build python from source Oct 30, 2014 at 14:34
  • Hey, it did work. Runnig, ./python did work. But, how to make it work using python3 in the terminal (by default)
    – devGeek
    Oct 30, 2014 at 15:09
  • @devGeek to replace your default version with 3.4.2, just run sudo make install. It will install 3.4.2 in /usr/local. Original 3.4.0 is still available with /usr/bin/python3. Oct 30, 2014 at 16:32
  • Sure, I already did sudo make install. Thanks! BTW the python3 opens 3.4.2 and for 3.4.0 python3m works.
    – devGeek
    Oct 30, 2014 at 17:38
0

If you need tkinter only for matplotlib, you can also use a different backend, like Egg: import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt

See more details here

0
sudo apt-get install python3-tk tk  

pyenv install 3.5.0  

that's it

1
  • 2
    What does this add to the existing, accepted answer? Oct 23, 2016 at 10:24
0

Just so you know, I'm using Ubuntu 16.04. Adding to the first answer do these things from the python file(After extraction):

./configure #(there will be a configure file)
make
make test
sudo make install

I had done these things the first time but still it was showing me these errors:

IDLE can't import Tkinter.  Your Python may not be configured for Tk.

while running python3 -m idlelib.idle from cmd.

So I did:

sudo apt-get install tk-dev

or you can do

sudo apt-get install tk8.6-dev

then again

./configure
make
make test
sudo make install

This solved the issue as the next time I ran python3 -m idlelib.idle, it opened the IDLE.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .