TypeError: 'module' object is not callable
socket is a module, containing the class
You need to do
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:
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:
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?"
- "The code is trying to call on
socket. That should be callable! Is the variable
socketis what I think it is?`
- I should print out what socket is and check
Assume that the content of YourClass.py is:
class YourClass: # ......
If you use:
from YourClassParentDir import YourClass # means YourClass.py
In this way, you will get TypeError: 'module' object is not callable if you then tried to call
But, if you use:
from YourClassParentDir.YourClass import YourClass # means Class YourClass
YourClass.YourClass(), it works.