Last updated: Apr 8, 2024
Reading timeยท5 min
The Python "TypeError: 'module' object is not callable" occurs when we import
a module as import some_module
but try to call it as a function or a class.
To solve the error, use dot notation to access a specific function or class
before calling it, e.g. module.my_func()
.
Here is an example of how the error occurs when importing built-in or third-party modules.
import math # โ๏ธ TypeError: 'module' object is not callable print(math(15.99))
A module is a collection of functions or classes.
We imported the math
module and tried to call it directly using parentheses
()
.
Instead, we are trying to call the entire module as a function.
To solve the error, use dot notation to access a function or a class and call the function instead.
import math print(math.floor(15.99)) # ๐๏ธ 15 print(math.ceil(15.99)) # ๐๏ธ 16
We used dot notation to access the floor()
and ceil()
methods of the math
module.
The error is resolved because we no longer call the entire module as a function.
Here is an example of how the error occurs with local modules. Imagine that we
have 2 files - another.py
and main.py
.
def do_math(a, b): return a + b
And here is the code for main.py
.
import another # โ๏ธ TypeError: 'module' object is not callable another(100, 100)
In the main.py
file, we import the another
module and directly call it.
Notice that we aren't actually calling the do_math
function, we are calling
the module.
One way to solve the error is to use dot notation to access the specific function or class before you call it.
import another print(another.do_math(100, 100)) # ๐๏ธ 200
We used dot notation to get access to the do_math
function from the
another.py
module before calling it.
Alternatively, you can import the specific function or class directly.
from another import do_math print(do_math(100, 100)) # ๐๏ธ 200
This import statement only imports the do_math
function from the another.py
module.
from another import first, second, third
.from another import first, second, third
A good way to start debugging is to print(dir(your_module))
and see what
attributes the imported module has.
Here is what printing the attributes of the another.py
module looks like.
import another # ๐๏ธ ['__builtins__', '__cached__', '__doc__', # '__file__', '__loader__', '__name__', # '__package__', '__spec__', 'do_math'] # ๐๏ธ notice 'do_math' print(dir(another))
If you pass a module object to the dir() function, it returns a list of names of the module's attributes.
Notice the do_math
attribute at the end of the list.
This means that we have to access the attribute as another.do_math
to get a
reference to the function.
import another print(another.do_math(50, 50)) # ๐๏ธ 100
Alternatively, you can import the do_math
function directly.
from another import do_math print(do_math(50, 50)) # ๐๏ธ 100
The error means that we are trying to call a module object instead of a function or a class.
To solve the error, we have to access an attribute on the module object that points to the specific function or class.
The Python "TypeError: 'module' object is not subscriptable" occurs when we
import a module as import some_module
but use square brackets to access a
specific key.
To solve the error, use dot notation to access the specific variable or import it directly.
Here is an example of how the error occurs. Imagine we have 2 files -
another.py
and main.py
.
my_dict = {'name': 'Alice', 'age': 30}
And here is the content for main.py
.
import another # โ๏ธ TypeError: 'module' object is not subscriptable print(another['name'])
The issue is that, in the main.py
file, we import the another
module but use
square brackets to try to access a key in the my_dict
variable.
One way to solve the error is to use dot notation to access the variable before we try to access a key (or index if using lists).
import another print(another.my_dict['name']) # ๐๏ธ "Alice"
We used dot notation to get access to the dict
object from another.py
before
trying to access a specific key.
Alternatively, you can import the my_dict
variable from the another.py
module.
from another import my_dict print(my_dict['name']) # ๐๏ธ "Alice"
This import statement only imports the my_dict
variable from the another.py
module.
from another import first, second, third
.A good way to start debugging is to print(dir(your_module))
and see what
attributes the imported module has.
Here is what printing the attributes of the another.py
module looks like.
import another # ๐๏ธ ['__builtins__', '__cached__', '__doc__', # '__file__', '__loader__', '__name__', '__package__', # '__spec__', 'my_dict'] ๐๏ธ notice 'my_dict' print(dir(another))
If you pass a module object to the dir() function, it returns a list of names of the module's attributes.
Notice the my_dict
attribute at the end of the list.
This means that we have to access the attribute as another.my_dict
to get a
reference to the dictionary.
import another print(another.my_dict) # ๐๏ธ {'name': 'Alice', 'age': 30}
Alternatively, you can import the my_dict
variable directly.
from another import my_dict print(my_dict) # ๐๏ธ {'name': 'Alice', 'age': 30}
Module objects are not subscriptable, so we have to access an attribute of the
module to get a hold of a dict
or a list
object (which are subscriptable).
You should only use square brackets to access subscriptable objects.
The subscriptable objects in Python are:
All other objects have to be converted to a subscriptable object by using the
list(),
tuple(), dict()
or str()
classes to
be able to use bracket notation.
Subscriptable objects implement the __getitem__
method whereas
non-subscriptable objects do not.
a_list = [1, 3, 5, 7] # ๐๏ธ <built-in method __getitem__ of list object at 0x7f71f3252640> print(a_list.__getitem__)