TypeError: 'module' object is not callable


Question

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?

1
463
3/27/2011 4:52:37 AM

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
486
12/26/2010 3:59:34 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon