Python Turtle error in python main modules - python

File "/usr/lib/python3.10/turtle.py", line 107, in
import tkinter as TK
ModuleNotFoundError: No module named 'tkinter'
Error occurring in linux python modules

In Arch, Tk is available in the Arch repository.
The following fixed it for me on Manjaro. Just type on the terminal:
pacman -S tk
If you are using one of Debian flavours, Ubuntu, Mint etc...
apt-get install tk

actually I found that this worked for UBUNTU for me (same for debian other flavours)
sudo apt-get install python3.10-tk
since I have python 3.10 you would need to use your version.
thanks to https://stackoverflow.com/a/18143036/1422486

Related

import _tkinter # If this fails your Python may not be configured for Tk error in python 3.8

Currently using Ubuntu 20.04 LTS with python3.8.5.
Its my first time using ubuntu with absolutely no previous knowledge of terminal.SO,would love to have a detailed answer if possible.
Below is terminal output when i try importing tkinter in python3.
>>> import tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.8/tkinter/__init__.py", line 36, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
>>>
I have reinstalled python3 and tkinter using sudo apt.But still it shows same error.
When i run the same command in python IDLE it works without any error.
I hope this explains my problem clearly, if any other info. is required pls reply.
I also tried running the command >>>from tkinter import *
I faced the same issue on MacOS. I was using a Python virtual environment. This command worked for me:
brew install python-tk
CentOS 8 + Python 3.9.5
following are what i have done:
1st:
yum search tkinter
yum install python39-tkinter.x86_64
2nd:
yum install tk-devel
3rd: cd python3.9.5 source folder
make install
it work in my case
since I'm still using python 3.9, this code works for me:
brew install python-tk#3.9
if using brew install python-tk brew will install python-tk#3.10 which is key-only
I had the same issue. Switched from 3.8 to 3.7 and it worked. I'm using PyCharm CE, which makes it easy to switch between interpreters.
Resolved the issue it occurred because the Tkinter was installed for version 3.5 and not for the 3.8 version. For that, I installed the 3.5 version and kept only one version i.e. 3.8, and installed Tkinter again, and it worked!
This is just a workaround to make things work, but the more preferred way is to create a venv and then install the particular versions of python and libraries that are needed.

install tkinter for python 3.7.3 in Ubuntu 18.04

When I import tinker in python 3.7.3 on Ubuntu 18.04:
>>> import tkinter
I got:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/tkinter/__init__.py", line 36, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
Then I install tk using both of the following:
sudo apt-get install python3-tk
It didn't work.
I also did all the things in Tkinter module not found on Ubuntu, still didn't work.
I noticed that when I do:
sudo apt-get install python3.7-tk
It says:
Note, selecting 'python3-tk' instead of 'python3.7-tk'
python3-tk is already the newest version (3.6.9-1~18.04).
Is tk automatically installed under python 3.6.9? How can I fix this?
I also saw a solution from https://wiki.python.org/moin/TkInter:
If it fails with "No module named _tkinter", your Python configuration needs to be modified to include this module (which is an extension module implemented in C). Do not edit Modules/Setup (it is out of date). You may have to install Tcl and Tk (when using RPM, install the -devel RPMs as well) and/or edit the setup.py script to point to the right locations where Tcl/Tk is installed. If you install Tcl/Tk in the default locations, simply rerunning "make" should build the _tkinter extension.
Could someone explain to do how to do the steps mentioned in this paragraph?
OK. I think the problem is that the newest version of tkinter for Ubuntu 18.04 is "python3-tk_3.6.9-1~18.04_i386.deb". Now I found that tk for python3.7.3 is available for other systems(e.g. python3-tk_3.7.3-1_amd64.deb). Can I download and use these ones on my system?
acw1668: Thanks this helped me a lot. I'm using Python 3.8. Using your method was able find where tkinter for Python 3.8 was install (/usr/lib/python3.8/). So i copied the files to (/usr/local/lib/python3.8/) which is where Python is installed on my computer. Now it'e working.

ModuleNotFoundError: No module named 'tkinter' while importing matplotlib Python 3.6(x64) [duplicate]

I tried to use the matplotlib package via Pycharm IDE on windows 10.
when I run this code:
from matplotlib import pyplot
I get the following error:
ImportError: No module named 'tkinter'
I know that in python 2.x it was called Tkinter, but that is not the problem - I just installed a brand new python 3.5.1.
EDIT: in addition, I also tried to import 'tkinter' and 'Tkinter' - neither of these worked (both returned the error message I mentioned).
For Linux
Debian based distros:
sudo apt-get install python3-tk
RPM based distros:
sudo yum install python3-tkinter
For windows:
For Windows, I think the problem is you didn't install complete Python package. Since Tkinter should be shipped with Python out of box. See: http://www.tkdocs.com/tutorial/install.html . Good python distributions for Windows can be found by the companies Anaconda or ActiveState.
Test the python module
python -c "import tkinter"
p.s. I suggest installing ipython, which provides powerful shell and necessary packages as well.
you can use
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
if you dont want to use tkinter at all.
Also dont forget to use %matplotlib inline at the top of your notebook if using one.
EDIT: agg is a different backend like tkinter for matplotlib.
For Windows users, there's no need to download the installer again. Just do the following:
Go to start menu, type Apps & features,
Search for "python" in the search box,
Select the Python version (e.g. Python 3.8.3rc1(32-bit)) and click Modify,
On the Modify Setup page click Modify,
Tick td/tk and IDLE checkbox (which installs tkinter) and click next.
Wait for installation and you're done.
On Centos, the package names and commands are different. You'll need to do:
sudo yum install tkinter
To fix the problem.
Almost all answers I searched for this issue say that Python on Windows comes with tkinter and tcl already installed, and I had no luck trying to download or install them using pip, or actviestate.com site. I eventually found that when I was installing python using the binary installer, I had unchecked the module related to TCL and tkinter. So, I ran the binary installer again and chose to modify my python version by this time selecting this option. No need to do anything manually then. If you go to your python terminal, then the following commands should show you version of tkinter installed with your Python:
import tkinter
import _tkinter
tkinter._test()
If you are using fedora then first install tkinter
sudo dnf install python3-tkinter
I don't think you need to import tkinter afterwards
I also suggest you to use virtualenv
$ python3 -m venv myvenv
$ source myvenv/bin/activate
And add the necessary packages using pip
On CentOS 7 and Python 3.4, the command is sudo yum install python34-tkinter
On Redhat 7.4 with Python 3.6, the command is sudo yum install rh-python36-python-tkinter
For windows users, re-run the installer. Select Modify. Check the box for tcl/tk and IDLE. The description for this says "Installs tkinter"
On Ubuntu, early 2018, there is no python3.6-tk on ubuntu's (xenial/16.04) normal distributions, so even if you have earlier versions of python-tk this won't work.
My solution was to use set everything up with python 3.5:
sudo apt install python3.5-tk
virtualenv --python=`which python3.5` python-env
source python-env/bin/activate
pip install -r requirements.txt
And now matplotlib can find tkinter.
EDIT:
I just needed 3.6 afterall, and the trick was to:
sudo apt install tk-dev
and then rebuild python3.6, after tk-dev, eg:
./configure
make
make install
If you are using python 3.6, this worked for me:
sudo apt-get install python3.6-tk
instead of
sudo apt-get install python3-tk
Which works for other versions of python3
For the poor guys like me using python 3.7. You need the python3.7-tk package.
sudo apt install python3.7-tk
$ python
Python 3.7.4 (default, Sep 2 2019, 20:44:09)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tkinter'
>>> exit()
Note. python3-tk is installed. But not python3.7-tk.
$ sudo apt install python3.7-tk
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
tix python3.7-tk-dbg
The following NEW packages will be installed:
python3.7-tk
0 upgraded, 1 newly installed, 0 to remove and 34 not upgraded.
Need to get 143 kB of archives.
After this operation, 534 kB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu xenial/main amd64 python3.7-tk amd64 3.7.4-1+xenial2 [143
kB]
Fetched 143 kB in 0s (364 kB/s)
Selecting previously unselected package python3.7-tk:amd64.
(Reading database ... 256375 files and directories currently installed.)
Preparing to unpack .../python3.7-tk_3.7.4-1+xenial2_amd64.deb ...
Unpacking python3.7-tk:amd64 (3.7.4-1+xenial2) ...
Setting up python3.7-tk:amd64 (3.7.4-1+xenial2) ...
After installing it, all good.
$ python3
Python 3.7.4 (default, Sep 2 2019, 20:44:09)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> exit()
On CentOS 6.5 with python 2.7 I needed to do: yum install python27-tkinter
Sometimes (for example in osgeo4w distribution) tkinter is removed.
Try changing matplotlib backend editing matplotlibrc file located in [python install dir]/matplotlib/mpl-data/matplotlibrc changing The backend parameter from backend: TkAgg to something other like backend: Qt4Aggas described here: http://matplotlib.org/faq/usage_faq.html#what-is-a-backend
Since I'm using Python 3.7 on Ubuntu I had to use:
sudo apt-get install python3.7-tk
Maybe you installed python from source. In this case, you can recompile python with tcl/tk supported.
Complie and install tcl/tk from http://www.tcl.tk/software/tcltk/download.html, I'll suppose you installed python at /home/xxx/local/tcl-tk/.
# install tcl
wget -c https://prdownloads.sourceforge.net/tcl/tcl8.6.9-src.tar.gz
tar -xvzf tcl8.6.9-src.tar.gz
cd tcl8.6.9
./configure --prefix=/home/xxx/local/tcl-tk/
make
make install
# install tk
wget -c https://prdownloads.sourceforge.net/tcl/tk8.6.9.1-src.tar.gz
tar -xvzf tk8.6.9.1-src.tar.gz
cd tk8.6.9.1
./configure --prefix=/home/xxx/local/tcl-tk/
make
make install
Recompile python with tcl/tk supported, for example:
# download the source code of python and decompress it first.
cd <your-python-src-dir>
./configure --prefix=/home/xxx/local/python \
--with-tcltk-includes=/home/xxx/local/tcl-tk/include \
--with-tcltk-libs=/home/xxx/local/tcl-tk/lib
make
make install
I had the same issue on Win x86/64 because my custom Python3.7 installation did not include Tcl packages, so just modify or re-install your python
https://www.python.org/downloads/release/python-370/
Download Python Setup file and click modify then tick the tcl/tk and install.
After installation is complete go to folder where python is installed ( Default is C:\Users*Your username*\AppData\Local\Programs\Python\Python39\Lib) .
Copy the tkinter folder and paste it in the lib folder of your pycharm project.
Error should be resolved
Follow these steps to easily install Tkinter on your PyCharm IDE:
First go to the File:
Second is go to New Project Setup > Settings for new projects:
And then click the Settings for new projects and you'll get redirected in here:
Please click the + symbol in there:
after that install future and there you go...
If you’re having pip(which you probably do), open up cmd or powershell on Windows or a terminal window on OS X or Linux and try this(make sure python is in the system path if you’re on Windows):
pip install tkinter
It should take a while to install tkinter, and then try to execute this code block:
from tkinter import *
root = Tk()
# Your code goes here
root.mainloop()
Hope that this helps! Thank you!

Python 3.5 tkinter won't install Ubuntu

Trying to run a script written for Python3.5 on Ubuntu 64 bit.
I'm 99% sure I installed Python3.5 correctly. It lives at /usr/local/bin. But I am getting this error when I try to run my script:
import tkinter as tk # python3
File "/usr/local/lib/python3.5/tkinter/__init__.py", line 35, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named '_tkinter'
I've tried many of the workarounds on here, but to no avail.
apt-get install python3-tk
Says it's up to date and
update-python-modules -a
seems to execute fine, so I really don't know why it's not finding tkinter...
Thanks in advance.
UPDATE: I just blew away the VM, so if someone could give me proper steps on installing Python3.5 + tkinter on Ubuntu 64-bit 14.04, that'd be awesome.
Thanks!

How to get tkinter working with Ubuntu's default Python 2.7 install?

I'm using Ubuntu 11.10, which came pre-installed with Python 2.7.3.
I installed the python3 package, and the python3-tk package, via apt-get install, and they worked together "out of the box".
But though I've installed the python-tk package, I can't figure out how to get Python2.7 to see it. I get the error message below when I try to import it.
import Tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
edit: I also ran the following based on Python-tk package not recognized in Python 2.7.3, and got:
$ file /usr/lib/libtk8.5.so.0
/usr/lib/libtk8.5.so.0: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, stripped
$ uname -a
Linux bugbot 3.0.0-23-generic-pae #39-Ubuntu SMP Thu Jul 19 19:39:19 UTC 2012 i686 i686 i386 GNU/Linux
I figured it out after way too much time spent on this problem, so hopefully I can save someone else the hassle.
I found this old bug report deemed invalid that mentioned the exact problem I was having, I had Tkinter.py, but it couldn't find the module _tkinter: http://bugs.python.org/issue8555
I installed the tk-dev package with apt-get, and rebuilt Python using ./configure, make, and make install in the Python2.7.3 directory. And now my Python2.7 can import Tkinter, yay!
I'm a little miffed that the tk-dev package isn't mentioned at all in the Python installation documentation.... below is another helpful resource on missing modules in Python if, like me, someone should discover they are missing more than _tkinter.
Building Python and more on missing modules
I had same issue with python3.4 (ImportError: No module named '_tkinter')
sudo apt-get install tk-dev
got to python source directory
./configure
make
sudo make install
The following solved the issue for me on Linux Mint 16:
sudo apt-get install tk-dev python-tk
I use virtualenv but did not want to compile python, in order to make the python support tk, python-tk should be installed.

Categories