TypeError: The '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? I'm confused.

What do you need to know to answer my question?

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'>

Notice that the error here is pretty easy to understand: if module object is not callable, you are probably calling a module object! What's a module object? It's the type of thing you get when you import a module.

But even if you don't understand that (which is fine, it's often confusing that classes, modules, functions etc are all just objects like everything else), there's a pretty easy way to start debugging:

  • "Hmm, module object is not callable. That sounds like I'm trying to call something that you can't call. I wonder what I'm trying to call?"
  • "Oh, I'm calling socket. That should be callable! I wonder if the variable socket is what I think it is?`
  • print socket