Getting error using Tkinter in python on mac OS X - python

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.

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.

Python Can't Find Wolframalpha Module

I'm tyring to import wolfamalpha into my code but it gives a error saying:
ModuleNotFoundError: No module named 'wolframalpha'
I tried installing it with pip install wolframalpha, but it still doesn't work.
Here is my code:
import wolframalpha
client = wolframalpha.Client('*************')
When you run your python file, are you sure that you are using the correct python interpreter that you performed the pip install wolframalpha to? It sounds like you may have multiple versions of python on your machine and there is a mismatch. If you are using VSCode, it is easy to see which interpreter is running on the bottom of the screen, which may help you debug the issue.

PyInstaller won't import pywin32 / win32clipboard - ImportError upon running executable

I'm working in Windows 10 with Python 3.8.6 and using PyInstaller 4.0 to compile my script as an executable for distribution. I just added a feature today that required importing win32clipboard. PyInstaller finishes compiling without any errors, but the excecutable fails to load due to:
ImportError: DLL load failed while importing win32clipboard: The specified module could not be found.
I attempted to compile the program again using the hidden-import flag:
pyinstaller myscript.py --onefile --hidden-import win32clipboard
This produced the same result and an ImportError upon trying to load the program (no errors during compiling).
I know that win32clipboard is part of pywin32 and my program compiled and ran without any issues prior to the code changes that required importing it. It still runs fine out of IDLE and functions as intended when using the win32clipboard-enabled features.
Is there some way to manually direct PyInstaller to import this correctly, or some other way to fix this issue and get the executable working again?
I was able to work around this issue by importing pywintypes into my script before win32clipboard.
import pywintypes
import win32clipboard
Found the suggestion in an old GitHub bug report for an issue people were having importing win32api with PyInstaller and decided to give it a try. I was able to compile and run my program without any issues after doing this.
Working with PyInstaller: 5.1, Python: 3.8.12 (conda), and Platform: Windows-10.
In addition to the solution from #DJT of importing pywintypes before win32clipboard, I solved the same error by downgrading my pywin32 from version 228 to version 225. Just type the following command with the building environment active:
pip install --upgrade --user pywin32==225

Using Tkinter on OSX Mountain Lion 8.2

I'm working on learning to make GUIs with Tkinter for Python programs and I have run into a bunch of problems:
Attempting to run from Tkinter import filedialog I get ImportError: cannot import name filedialog. Elsewhere I have seen it called with the lowercase tkinter (which I think is for Python 3 only) but it does not recognize that as existing ImportError: No module named tkinter. Yet running from Tkinter import * works and I'm able to create the sample hello world from the tutorial.
My only guess is that it is attempting to use an obsolete version of Tkinter that shipped with OSX. Yet considering how new Mountain Lion is, I have to wonder how obsolete it would really be. My assumption is that upon installing Python 2.7 myself, if I run from IDLE, it should work, but instead I get exactly the same thing above from the IDLE shell.
So after that I tried sudo pip install tkinter and sudo pip install Tkinter from the Unix terminal, but I get back No distributions at all found for update
So I start looking into documentation on Tkinter itself, and I come across Tcl/Tk instructions for Mac OS X which states:
Important: If you are using Mac OS X 10.8, 10.7 or 10.6, use IDLE or tkinter from a 64-bit/32-bit Python installer only with ActiveTcl 8.5 installed. If you cannot install ActiveTcl 8.5, use a 32-bit-only installer instead.
So I find my OS in the table they provide and see that they recommend ActiveTcl8.5.11, but unfortunately, the download link is broken.
So I'm completely at a loss here. Any help would be appreciated.
If you want to implement a File Dialog, have a look at tkFileDialog. It is a seperate module & not part of Tkinter module.
You can use it by directly importing it:
import tkFileDialog

Error Importing GTK

I'm running Python 2.7, with the latest version of GTK installed.
I'm trying to import gtk with the following line of code:
import gtk
It is throwing the following error:
Traceback (most recent call last): File
"C:\GTKTutorial\tutorial.py", line 3, in
import gtk File
"C:\Python27\lib\site-packages\gtk-2.0\gtk__init__.py", line 40, in
from gtk import _gtk ImportError: DLL load failed: The specified
procedure could not be found.
How do I fix this? import pygtk is working, and I have Glade 3.8.0 installed.
Where did you get the PyGTK installer from? Try using this one: http://ftp.gnome.org/pub/GNOME/binaries/win32/pygtk/2.22/pygtk-all-in-one-2.22.6.win32-py2.7.msi
I just spent 3 days debugging this issue on my computer. This problem can have many root causes because importing gtk actually causes a lot of DLLs being loaded. If any of them fails, you get the same error message. For me, the break-through was
http://www.dependencywalker.com/
which I used to profile the command
python -i -c "import gtk"
On my PC, a wrong version of zlib1.dll was in the system32 directory, which prevented the right one in gtk\bin from loading. And it did that even though the first entry in my path pointed to gtk\bin.
I deleted the zlib1.dll from system32 (the application that put it there deserves to die) and the import worked fine. Your problem may be different, but dependency walker probably can give you a hint on what is going wrong.
Windows can have other reasons for loading a DLL other than the one you put in your directory or path. I found the blog
http://www.davidlenihan.com/2007/07/winsxs.html
useful. It describes Microsoft's solution for managing many versions of DLLs, and how to troubleshoot problems with it.

Categories

Resources