Python3 ImportError: No module named '_tkinter' [duplicate] - python

This question already has answers here:
python ImportError: No module named Tkinter
(5 answers)
Closed 4 years ago.
On my Linux Mint 18, I've tried to install Python 3.6.1 beside my 3.5.2.
With these commands:
wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
tar xJf Python-3.6.1.tar.xz
cd Python-3.6.1
./configure
make
make install
The installation was successfully but, now, every time I try to import tkinter, I have the same error:
>>> from tkinter import tk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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 no idea how to get rid of this issue, and how remove the 3.6.1

I think you still need to install the tkinker package. You can do this by simply typing:
sudo apt-get install python3-tk

The issue as I see is that you are still calling your python3.5 binaries which might be set as default python interface. See the line in your error which tells the version of python it is referring to:
/usr/local/lib/python3.5/tkinter/
If it's a UNIX / Linux flavour you are using, you can check where are your python binaries by using
whereis python
and you will get a list of all the flavours and places it is in:
You simply call out your chosen flavor to work with, which I am guessing might be
/usr/local/bin/python3.6
and then list the available modules to check if Tkinter is available or not, although it is highly unlikely not to as it comes bundled as a standard library.

If you are using pycharm then you can simply write:
from tkinter import *

Related

Running Tkinter on Mac

I am an absolute newbie. I'm trying to make Python GUI for my school project so I decided to use Tkinter. When I try to import Tkinter it throws this message:
>>> import tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/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'
I tried to find a solution online but I couldn't figure it out (mostly didn't understand it).
I read about some problem with directory in setup.py but I don't understand how to fix it. I have tkinter folder in my python3.7 folder.
I don't really understand these steps that I found:
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.
I'm using Mac OS and use Visual Studio Code.
To check your python version, run this on terminal:
$ python --version
Check what python your machine is using. Run:
$ which python
If it's using python from Homebrew, it's probably using Python 2. If you installed python 3 manually, just install tKinter manually.
$ brew install python-tk
To run python 2 manually, run on terminal:
$ python <FILENAME>
If python 3, run:
$ python3 <FILENAME>
https://stackoverflow.com/a/9883299/4678635 will help you.
In summary, you have to reinstall python for your computer bit system.
And below code strategy is also helpful to you.
try:
from Tkinter import * # for Python2
except ImportError:
from tkinter import * # for Python3

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.

No module named 'index' after install pyflann

I have some problems installing pyflann in python 3.7.3, after execute:
pip install pyflann
The installation is successfully accomplished, but when I import the library and a run a python program, I got this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Anaconda3\lib\site-packages\pyflann\__init__.py", line 27, in <module>
from index import *
ModuleNotFoundError: No module named 'index'
I'm using window 10. How can I fixed?
pyflann does not have support for python 3 yet, according to this GitHub issue. Your two options are:
Install the pyflann-py3 package:
pip install pyflann-py3
Or, you could take the advice from the issue and use 2to3:
sudo 2to3 -w D:\Anaconda3\lib\site-packages\pyflann
The pyflann package is not compatible with Python 3. It looks like
it is not actively maintained, the last commit was in February 2017:
https://github.com/primetang/pyflann/commits/master
There is an open issues documenting the problem:
https://github.com/primetang/pyflann/issues/1
There are a few open PRs trying to add Python 3 support that were never
merged:
https://github.com/primetang/pyflann/pulls
You might have better luck with one of the forks, but I can't vouch for them,
I've never used pyflann:
https://github.com/primetang/pyflann/network/members

Python 2.6.6 and tkinter module

I need to run an application written in python. When I try to load it from terminal I get this error:
File "/usr/local/bin/soar", line 3, in <module>
import form.main
File "/usr/local/lib/python2.6/site-packages/form/main.py", line 14, in <module>
from Tkinter import *
File "/usr/local/lib/python2.6/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
I installed python 2.6.6 from the source files. I am really confused!!
How should I install tkinter?
I am running Xubuntu 13.10. You can see the different versions of python already installed in the below:
How can I remove python2.6.6? I tried to remove all files and folders related to python2.6. Now when I run python, it still loads python 2.6.6 I really have no idea what to do!! I tried to follow the instructions here and here to uninstall it but it did not work, that's why I deleted all the files and folders manually.
Type the following in your terminal:
sudo apt-get install python-tk
Install python3-tk package if you need Tkinter functionality in the 3rd version of python.

Cannot get mapnik working with python3, but it does with python2

Here is a post about installing a module in python3. When I use brew install python, then it installs it for 2.7.
When I use the method suggested by dan, which aimed to install it directly in python3 (who i really thank), but which didn't work :
# Figure out the path to python3
PY3DIR=`dirname $(which python3)`
# And /then/ install with brew. That will have it use python3 to get its path
PATH=$PY3DIR:$PATH brew install mapnik
The installation was successful but in python2. so I get:
For non-homebrew Python, you need to amend your PYTHONPATH like so: export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
so i finally add the path manually in python3 :
import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')
I get this error :
Traceback (most recent call last): File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/mapnik/__init__.py", line 69, in <module>
from _mapnik import * ImportError: dlopen(./_mapnik.so, 2): Symbol not found: _PyClass_Type Referenced from: ./_mapnik.so
Expected in: flat namespace in ./_mapnik.so
Please help, I have spent so many hours on this ...
Thanks!!!
The Mapnik python bindings depend on boost_python. And both need to use the same python. The problem is likely that homebrew is providing a bottle of boost which includes boost python built against python 2.7 and not python 3.x.

Categories