I am trying to import Tkinter. However, I get an error stating that Tkinter has not been installed:
ImportError: No module named _tkinter, please install the python-tk package
I could probably install it using synaptic manager (can I?), however, I would have to install it on every machine I program on. Would it be possible to add the Tkinter library into my workspace and reference it from there?
Actually, you just need to use the following to install the tkinter for python3:
In addition, for Fedora users, use the following command:
It is not very easy to install Tkinter locally to use with system-provided Python. You may build it from sources, but this is usually not the best idea with a binary package-based distro you're apparently running.
It's safer to
apt-get install python-tk
on your machine(s). (Works on Debian-derived distributions like for Ubuntu; refer to your package manager and package list on other distributions.)tk-devel also needs to be installed in my case
install these and rebuild python
If, like me, you don't have root privileges on your network because of your wonderful friends in I.S., and you are working in a local install you may have some problems with the above approaches.
I spent ages on Google - but in the end, it's easy.
Download the tcl and tk from http://www.tcl.tk/software/tcltk/download.html and install them locally too.
To install locally on Linux (I did it to my home directory), extract the .tar.gz files for tcl and tk. Then open up the readme files inside the ./unix directory. I ran
It may seem a pain, but the files are tiny and installation is very fast.
Then re-run
python setup.py build
andpython setup.py install
in your python installation directory - and it should work. It worked for me - and I can now import Tkinter etc to my heart's content - yipidy-yay. An entire afternoon spent on this - hope this note saves others from the pain.The situation on macOS is still a bit complicated, but do-able:
Python.org strongly suggest downloading tkinter from ActiveState, but you should read their license first (hint: don't redistribute or want Support).
When the download is opened OS X 10.11 rejected it because it couldn't find my receipt: "ActiveTcl-8.6.pkg can’t be opened because it is from an unidentified developer".
I followed an OSXDaily fix from 2012 which suggested allowing from anywhere. But OS X has now added an "Open Anyway" option to allow (e.g.) Active-Tcl as a once off, and the "Anywhere" option has gained a timeout.
For Python 2.7:
as it says here. In my case, on Windows, what helped was reinstalling the Python distribution. Probably a long time ago I had unchecked the "Tcl/Tk" installation feature. After reinstalling all works fine and I can
import _tkinter
andimport Tkinter
.