Running Tkinter on Mac - python

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

Related

Tkinter unable to configure

I have this problem when I try to run an app that imports turtle:
Traceback (most recent call last):
File "/home/panos/Documents/python/challenge_60.py", line 1, in <module>
import turtle
File "/usr/local/lib/python3.9/turtle.py", line 107, in <module>
import tkinter as TK
File "/usr/local/lib/python3.9/tkinter/__init__.py", line 37, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
I have done the below commands:
sudo apt-get install python3-tk
pip3 install tkinter
pip3 install PythonTurtle
pip3 install Tcl
sudo apt-get install python-tk python3-tk tk-dev
sudo apt-get install python3-PythonTurtle
And, although it is installed, the error still exists. I haven't understand what does wiki says. Could you explain, with all the details, what do I have to do, because I am a newbie at Linux and commands in general?
This error is probably made, because you have installed the tkinter , for instance, on python 3.7, but you are running your code with an interpreter based on python 3.8.
Another reasons, like installing the library in a virtual environment and trying to run the code out of the venv, can also
lead to this error.
Make sure pip and python command are in the same location/path
to make sure they are from the same SDK.
This command indicates where python is installed on your machine:
(Unix Only)
which python
Windows users, use this snippet below to find path of the installed python :
import sys
locate_python = sys.exec_prefix
print(locate_python)
Check the pip location using the which command as well.
which pip
If pip and python are in the same path, you probably won't get the error anymore.
First you have to pay attention to a few things 1- All the packs must be updated and installed If you have downloaded the packs, update them, especially the pip module that is for installation 2- Note that the spelling of the words you use is correct 3 - Maybe the code you write does not support that module, you should also pay attention to this issue 4- Finally, there are some problems in your code. First, to download the Tkinter module, you have to write pip3 install tk or pip install tk. You should not write the word Tkinter yourself. Update the latest version and then try again. If it still does not work, turn on your Windows 10 (if not) and finally the last solution is to change your ide vs code. The best ide for this situation. Is And you can also help to install and import the library of online articles. If you use pycharm, enter the Windows command line and type pip3 install future or pip install future, and then the thinner will be imported for you, and then enter the following command: from future.moves import tkinter and you can find how to install each house in different ideas with a simple search

ModuleNotFoundError: No module named 'pynput' Python3 and pip3

I want to use 'pynput', so I used pip to add it to my environment.
The installation proceeds without problem.
But I am unable to import it into my project.
I am using python 3.8.1 on my environment.
I used pip3 for installation.
I have already tried to install pynput, uninstall it and reinstall it multiple times.
My .py file doesn't have a confusing name like "pynput.py"
I am comfortable with my environment when I try to execute my file.
I am trying to run from my terminal or VSCodium, and neither of them works.
And I work on Debian 10.
pip freeze :
pynput==1.6.7
python-xlib==0.26
six==1.14.0
Traceback :
Traceback (most recent call last):
File "./play.py", line 6, in <module>
from pynput import keyboard
ModuleNotFoundError: No module named 'pynput'
So I don't understand why it doesn't work.
thank you in advance for your help :)
When creating my project, I was not working under an environment, so I used the classic shebang: #!/bin/python3.
Then, I went under an environment to use pynput, but I just forgot to change my shebang to #!/usr/bin/env python.
So, actually, I didn't risk finding pynput
It might be possible you have two versions. Rry installing with python3 -m pip install pynput or you should use some older version of Python. I am using 3.7.5 and its works perfect for me.
Try importing from the terminal.

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.

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

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 *

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.

Categories