TypeError: 'module' object is not callable in Python [Fixed]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
5 min

banner

# Table of Contents

  1. TypeError: 'module' object is not callable in Python
  2. TypeError: 'module' object is not subscriptable in Python

# TypeError: 'module' object is not callable in Python

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().

typeerror module object is not callable

Here is an example of how the error occurs when importing built-in or third-party modules.

main.py
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 ().

The error is caused because we aren't accessing a specific function or class in the module.

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.

main.py
import math print(math.floor(15.99)) # ๐Ÿ‘‰๏ธ 15 print(math.ceil(15.99)) # ๐Ÿ‘‰๏ธ 16

using math floor and math ceil

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.

A module is simply a collection of functions and classes, so a module cannot be invoked directly.

# Solve the error when working with local modules

Here is an example of how the error occurs with local modules. Imagine that we have 2 files - another.py and main.py.

another.py
def do_math(a, b): return a + b

And here is the code for main.py.

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.

# Use dot notation to access the function or class before calling it

One way to solve the error is to use dot notation to access the specific function or class before you call it.

main.py
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.

# Importing the specific function or class directly

Alternatively, you can import the specific function or class directly.

main.py
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.

If you need to import multiple variables or functions, separate them by commas in the import statement, e.g. from another import first, second, third.
main.py
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.

main.py
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.

main.py
import another print(another.do_math(50, 50)) # ๐Ÿ‘‰๏ธ 100

Alternatively, you can import the do_math function directly.

main.py
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.

# TypeError: 'module' object is not subscriptable in Python

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.

typeerror module object is not subscriptable

Here is an example of how the error occurs. Imagine we have 2 files - another.py and main.py.

another.py
my_dict = {'name': 'Alice', 'age': 30}

And here is the content for main.py.

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.

# Use dot notation to access the variable before accessing a key

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).

main.py
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.

# Directly import the dictionary from the module

Alternatively, you can import the my_dict variable from the another.py module.

main.py
from another import my_dict print(my_dict['name']) # ๐Ÿ‘‰๏ธ "Alice"

This import statement only imports the my_dict variable from the another.py module.

If you need to import multiple variables or functions, separate them by commas in the import statement, e.g. 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.

main.py
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.

main.py
import another print(another.my_dict) # ๐Ÿ‘‰๏ธ {'name': 'Alice', 'age': 30}

Alternatively, you can import the my_dict variable directly.

main.py
from another import my_dict print(my_dict) # ๐Ÿ‘‰๏ธ {'name': 'Alice', 'age': 30}
The "TypeError: object is not subscriptable" means that we are using square brackets to either access a key in a specific object or to access a specific index, however, the object doesn't support this functionality.

# Module objects are not subscriptable

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:

  • list
  • tuple
  • dictionary
  • string

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.

main.py
a_list = [1, 3, 5, 7] # ๐Ÿ‘‡๏ธ <built-in method __getitem__ of list object at 0x7f71f3252640> print(a_list.__getitem__)
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev