Atlas
Atlas

Reputation: 21

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

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 ?

Upvotes: 1

Views: 593

Answers (2)

Kartik Gupta
Kartik Gupta

Reputation: 21

make sure to not make your file with the name tkinter.py I had tried everything else but this was the problem.

Upvotes: 0

Talon
Talon

Reputation: 1884

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.

Upvotes: 2

Related Questions