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

devGeek
  • 594

4 Answers4

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
>>> 
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

Noam Peled
  • 1,050
0
sudo apt-get install python3-tk tk  

pyenv install 3.5.0  

that's it

muru
  • 207,228
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.

muru
  • 207,228