0

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?

Moonstone5629
  • 58
  • 1
  • 6
  • 4
    You named your script `tkinter.py`, rename it to other name. – acw1668 Jan 20 '21 at 03:01
  • Could be https://stackoverflow.com/questions/20998275/import-with-files-names-conflict, but since this is Python 3, it must be something wrong with pydev/anaconda.. – user202729 Jan 20 '21 at 03:03
  • Hi, yes apparently it is that my file name was creating a conflict. It worked when I changed it. Thanks for the info! – Moonstone5629 Jan 20 '21 at 03:06

1 Answers1

4

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.