Python “TypeError: ‘module’ object is not callable” Error and Solution – POFTUT

Python “TypeError: ‘module’ object is not callable” Error and Solution


Python functions are generally provided by the modules. Modules provide the ability to categorize different functions, variables etc. When we are dealing with modules and function we may get an error like TypeError: 'module' object is not callable.

What Is “TypeError: ‘module’ object is not callable”

While using the functions we generally use modules to import and use them. This may create confusion. In some cases, the module name and function name may be the same. For example getopt module provides the getopt() function which may create confusion.

We can see in the following screenshot the TypeError:'module' object is not callable. callable means that given python object can call like a function but in this error, we warned that a given module cannot be called like a function.

What Is "TypeError: 'module' object is not callable"
What Is “TypeError: ‘module’ object is not callable”

Solve By Importing Function

We can solve this error by important the function directly in the begging of the source code below the import statement part. We will use from and import statements. from is used to specify the module name and import is used to point function name. In this example, we will import the function namedchdir()from theos` module like below.

from os import chdir

...

...

...

chdir("/bin/")

Alternatively, we can set a new name to the imported function named chdir() . We will use as statement to specify a new name which is ch in this example.

from os import chdir as ch

...

...

...

ch("/bin")

Solve By Calling with Module Name

Another solution to the TypeError: 'module' object is not callable error is directly calling the function after importing required library. In this example, we will use import statement by specifying the module name and then use the module name with the function we want to call. In this example we will import the module os and run function chdir() from os module.

import os

# ...
# ...
# ...

os.chdir("/bin/")

LEARN MORE  Python Try Catch Exceptions Tutorial

Leave a Comment