+1 vote
in Python by
How to Fix TypeError: 'module' object is not callable

File "C:\Users\Administrator\Documents\Mibot\oops\blinkserv.py", line 82, in __init__

    self.serv = socket(AF_INET,SOCK_STREAM)

TypeError: 'module' object is not callable

Why am I getting this error?

1 Answer

0 votes
by
socket is a module, containing the class socket.

You need to do socket.socket(...) or from socket import socket:

>>> import socket

>>> socket

<module 'socket' from 'C:\Python27\lib\socket.pyc'>

>>> socket.socket

<class 'socket._socketobject'>

>>>

>>> from socket import socket

>>> socket

<class 'socket._socketobject'>

This is what the error message means:

It says module object is not callable, because your code is calling a module object. A module object is the type of thing you get when you import a module. What you were trying to do is to call a class object within the module object that happens to have the same name as the module that contains it.

Here is a way to logically break down this sort of error:

1) "module object is not callable. Python is telling me my code trying to call something that cannot be called. What is my code trying to call?"

2) "The code is trying to call on socket. That should be callable! Is the variable socket is what I think it is?`

3) I should print out what socket is and check print socket

Related questions

0 votes
asked Dec 14, 2019 in Python by sharadyadav1986
0 votes
asked May 17, 2020 in Python by SakshiSharma
...