Home > Mobile >  root = Tk() TypeError: 'module' object is not callable
root = Tk() TypeError: 'module' object is not callable

Time:03-07

hello everyone im a newbie at python and i learn tkinter so easy on me

from tkinter import *
root = Tk()

mylabel = Label(root, text="Hello world :)")

mylabel.pack()

root.mainloop()

this is my code when I run this code

Traceback (most recent call last):
  File "e:\New Folder Of Desktop\Python Project\gui.py", line 1, in <module>    
    from tkinter import *
  File "e:\New Folder Of Desktop\Python Project\tkinter.py", line 4, in <module>
    root = Tk()
TypeError: 'module' object is not callable

this error showup can someone help me pls ?

CodePudding user response:

Expansion from previous comment

Let this be your Directory structure:

E:\New Folder Of Desktop\Python Project\
 - gui.py
 - tkinter.py

In gui.py, you have the like from tkinter import *. When python does this, is searches multiple directories for some module with the name tkinter. You can see the list of places where this module is searched by checking sys.path

import sys
print(sys.path)

The first place searched is the current directory, the place where your installed modules are is typically near the end. When you have a python file with the same name in the current directory, the search is stopped and this will be imported instead of the one you installed. Some more info on importing here.

Renaming the conflicting file is the easiest approach.

You could mangle with the sys.path list and remove the current directory or such tricks, but I would not recommend that.

  • Related