Reputation: 58
I have an Anaconda environment with python 3.9.1 and I'm getting the following error message when I try to import tkinter.
Traceback (most recent call last):
File "/Users/isevilla/opt/miniconda3/envs/lab/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 3418, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-537f0be70a2b>", line 1, in <module>
runfile('/Users/isevilla/Documents/Tkinter playground/tkinter.py', wdir='/Users/isevilla/Documents/Tkinter playground')
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/isevilla/Documents/Tkinter playground/tkinter.py", line 1, in <module>
import tkinter as tk
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "/Users/isevilla/Documents/Tkinter playground/tkinter.py", line 4, in <module>
root = tk.Tk()
AttributeError: partially initialized module 'tkinter' has no attribute 'Tk' (most likely due to a circular import)
The error pops up when I'm trying to run the following simple code:
import tkinter as tk
root = tk.Tk()
myLabel = tk.Label(root, text='Hello world')
myLabel.pack()
root.mainLoop()
Does someone know why this happen and how can I fix it?
Upvotes: 0
Views: 839
Reputation: 56
The problem is that you named your file Tkinter.py. When you do the import, python sees the file and tries to import it instead of the Tkinter library.
Rename your file, and be sure to remove Tkinter.pyc if it exists.
Upvotes: 4