我有一个使用Python3.9.1的Anaconda环境,当我尝试导入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)
当我尝试运行以下简单代码时,会弹出错误:
import tkinter as tk
root = tk.Tk()
myLabel = tk.Label(root, text='Hello world')
myLabel.pack()
root.mainLoop()
有人知道为什么会发生这种情况吗?我该如何解决
问题是您将文件命名为Tkinter.py。在执行导入时,python会看到该文件并尝试导入它,而不是导入Tkinter库
重命名文件,并确保删除Tkinter.pyc(如果存在)
相关问题 更多 >
编程相关推荐