ImputError: No module named 'tkinter' - python

I am new to python and wanted to code a simple application to practice a bit more but for whatever reason it is saying there is no module named 'tkinter'. I've tested to make sure tkinter is installed using python -m Tkinter and it works but tkinter won't work with my project. I am using pop_os! Linux and I am using Visual Studio Code as my IDE
Traceback (most recent call last):
File "first_game.py", line 2, in <module>
from tkinter import *
ImportError: No module named tkinter

Capitalisation matters when it comes to names in Python. In Python 2, the library is called Tkinter. In Python 3, it has been renamed tkinter to be consistent with the rest of the standard library.
You can check which version of Python your command line is running with python --version, or by running the following script:
import sys
print(sys.version)

If python -m Tkinter works, then you need to do from Tkinter import *. Case matters when importing packages.
However, if python -m Tkinter works that means you're using python 2.x which is no longer supported. It would be best to switch to python 3.x, in which case the package is named tkinter rather than Tkinter.

Related

using correct python interpreter to use tkinter in jupyter notebook

I'm fairly new to all of this and trying to self-teach. I only have really a vague understanding of concepts like virtual environments, shells, and kernels. I'm currently trying to generate a GUI with tkinter in Jupyter Notebooks. I was having trouble doing this in Pycharm until I changed to a different interpreter. I don't fully understand why this resolved the problem, but I believe it had something to do with using the proper version of python to interpret the script. Unfortunately, I need my python scripts to run in Jupyter, but I can't get the tkinter module to work when I run it in a notebook. I'm not sure how to change the interpreter or if that would even be what I need to do to fix the problem.
Here's the error:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-f463ac1a0eae> in <module>
1 #Creating GUI with tkinter
----> 2 from tkinter.constants import *
3
4
5 def send():
/usr/local/Cellar/python#3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py in <module>
35 import types
36
---> 37 import _tkinter # If this fails your Python may not be configured for Tk
38 TclError = _tkinter.TclError
39 from tkinter.constants import *
ModuleNotFoundError: No module named '_tkinter'
Any help would be greatly appreciated!
See, your error specifies that if python fails to import _tkinter,your Python may not be configured for Tk. You need to edit your python installation.
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.

Cannot use tkinter in Python 3 on macOS

Short version: Python 3 is unable to use tkinter on my iMac.
Details and useful info: I'm on macOS Mojave, Version 10.14.3 (18D109). (I can't upgrade for a while yet because of software that won't run on the newer versions.)
In my scripts I'm using #!/usr/bin/env python3 and when I get to the line import tk as tkinter I get this error:
Traceback (most recent call last):
File "/Users/hal/Documents/Dev/HalPy/TestScript.py", line 5, in <module>
import tkinter
File "/Users/hal/.pyenv/versions/3.7.3/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'
When I tested it, from the command line, I typed python and got the Python shell and the version info (Python 3.7.3 (default, Apr 11 2020, 02:53:05)). Then I tried import tkinter as tk and I get this:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/hal/.pyenv/versions/3.7.3/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 see the module when I drop into the Help mode and type modules and tkinter shows up in the list of modules. Just in case, I tried installing it with pip, but it wouldn't install (which is what I expected).
So what do I need to do to get Python 3 to recognize tkinter? (Just in case it matters, Python 2 with Tkinter does work.)
Also, I have looked at this question and this one and this is NOT a duplicate. (First note that neither has an accepted answer.) The first one involves reinstalling, and the focus seems to be on Windows, and this is on macOS, which includes it pre-installed. I'm a bit concerned what reinstalling it would do. The 2nd question is having problems because it's being run within the XCode framework and one element of the issue is the wrong shebang and I'm already using the one the answer suggests.
you can find your answer here:
https://blog.lanzani.nl/2020/install-tkinter-macos/
If you are using a Python from any current python.org Python installer for macOS (3.9.0+, 3.8.0+, or 3.7.2+), no further action is needed to use IDLE or tkinter. A built-in version of Tcl/Tk 8.6 will be used.
If you are using macOS 10.6 or later, the Apple-supplied Tcl/Tk 8.5 has serious bugs that can cause application crashes. If you wish to use IDLE or Tkinter, do not use the Apple-supplied Pythons. Instead, install and use a newer version of Python from python.org or a third-party distributor that supplies or links with a newer version of Tcl/Tk.

windows tkinter install python 3.7.4

installed PYTHON 3.7.4 from the python.org website. Working on a windows 10 machine. everywhere it says tkinter should be part of it but it seems like it's not. I need help installing it.
This is the official documentation for the standard library Tkinter created by the folks at Python Software Foundation :
Tkinter
This will definitely help you install, import and get started with Tkinter on your Windows system.
For importing errors while trying to use Tkinter, please note :
The root of the problem is that the Tkinter module is named Tkinter (capital "T") in python 2.x, and tkinter (lowercase "t") in python 3.x.
To make your code work in both Python 2 and 3 you can do something like this:
try:
# for Python2
from Tkinter import *
except ImportError:
# for Python3
from tkinter import *
But since you've already mentioned that you're on Python 3.7.4, this import statement will suffice :
import tkinter as tk
Tkinter is built in with every Python installation.
I don't know if you typed the command wrong, but this is the proper way.
import Tkinter as tk
Note: There is a Capital T at the beginning of Tkinter.
In Python 3 for you,
import tkinter as tk
Note: You might accidentally deleted it in the folder of python so you could just download python again.
Below from https://pypi.org/project/PySimpleGUI/:
Python 3.7
If you must run 3.7, try 3.7.2. It does work with PySimpleGUI with no known issues.
PySimpleGUI with Python 3.7.3 and 3.7.4+. tkinter is having issues with all the newer releases. Things like Table colors stopped working entirely. It's unclear if there's been a change that makes the tkinter API no longer working which means they are not backwards compatible. That would be a real shame. If so, more work needs to be done in PySimpleGUI
Can you import tkinter by import tkinter?

Pyinstaller, .py version works, .exe "No module named Tkinter"?

I don't understand how this happened. myscript.py works, Tkinter is installed. However once I converted it to .exe using pyinstaller, I ran myscript.exe, I got a message in command prompt saying "No module named Tkinter".
How can I fix this?
I've already tried to convert it several times, tried using --noupx, and also tried reinstalling pyinstaller. Thanks
PyInstaller - Hidden Imports
Try using the --hiddenimport flag. Do --hiddenimport=Tkinter, or replace the Tkinter with whatever module you need. The issue seems reoccurring throughout the PyInstaller community. Here is one article.
Name Space
Tkinter has changed its namespaces from Python 2 to Python 3. It's now named tkinter in Python 3 and Tkinter in Python 2. This is the code I like to use so it's cross-version.
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
Then you can reference Tkinter as tk. Or if you do from Tkinter import * you can just use everything without a namespace. See the list here for more of the name changes between version.

Getting error using Tkinter in python on mac OS X

I tried to run a nltk code for drawing parse trees. I got the error that tkinter module is not installed.
These are the error messages I got:
1. UserWarning: nltk.draw package not loaded (please install Tkinter library).
warnings.warn("nltk.draw package not loaded")
2. import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
After some searches I installed the ActiveTcl 8.5.18.0 using this instructions.
But when I try to run my code I still get the sam error. I tried
import Tkinter
but I get the second error message above again.
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/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
I also looked at Tkinter documentations and it is mentioned that the correct installation of Tkinter can be verified by running the following command which again gives me another error.
command: python -m tkinter
error: /usr/local/opt/python/bin/python2.7: No module named tkinter
I found this answers on Stackoverflow for my problem but they are either not very clear or not applicable to my case.
How to get tkinter working with Ubuntu's default Python 2.7 install?
Problems: tk-dev is not available for OS X (it is same as ActiveTcl) and I couldn't figure out how to rebuilt my python using make
Tkinter: "Python may not be configured for Tk"
Problems: very vague. I don't know what should I do
please help.
You should install ActivePython rather than ActiveTcl, and use it as your preferred Python.
The problem is your Python install isn't picking up your Tcl install, and the simplest way to solve that is to install a Python version that is configured for Tk, which ActivePython is: http://www.activestate.com/activepython
The issue is that the _tkinter Python module is not installed in your build, which is a required bridge between Python and Tk. You will have to reinstall nltk and any other packages you are using, unfortunately, as the versions you have will be installed for your current Python and not your new one.

Categories

Resources