[pythonpune] socket - 'module' object is not callable

1,748 views
Skip to first unread message

ajax...@gmail.com

unread,
Oct 14, 2013, 12:09:57 PM10/14/13
to pytho...@googlegroups.com
Hello all ,

I am trying to run  a program , but I am getting following error

Traceback (most recent call last):
  File "socket.py", line 28, in <module>
    check = check_server(options.address, options.port)
  File "socket.py", line 8, in check_server
    s = socket.socket()
TypeError: 'module' object is not callable



Program

#!/usr/bin/env python
import socket
import re
import sys

def check_server(address, port):
   
    s = socket.socket()
    print "Attempting to connect to %s on port %s" % (address , port)
    try:
        s.connect((address, port))
        print "Connected to %s on port %s" % (address, port)
        return True
    except socekt.error, e:
        print "Connecton to %s on port %s failed: %s" % (address, port , e)
        return False

if __name__ == '__main__':
    from optparse import OptionParser
    parser = OptionParser()
   
    parser.add_option("-a", "--address", dest="address", default='localhost', help="ADDRESS for server", metavar="ADDRESS")
   
    parser.add_option("-p", "--port", dest="port", type="int", default=80, help="PORT for server", metavar="PORT")
   
    (options, args) = parser.parse_args()
    print 'options: %s, args: %s' % (options, args)
    check = check_server(options.address, options.port)
    print 'check_server returned %s' % check
    sys.exit(not check)


I am using Ubuntu 13.04 , python 2.7.
I also try to run some simple socket programs but i am getting  same error. Do I need to install any library? I tried installing python-socketpool , python-gevent.

I am python beginner. where i am making mistake?
Please help .

Vineet Naik

unread,
Oct 14, 2013, 1:32:39 PM10/14/13
to pytho...@googlegroups.com
On Mon, Oct 14, 2013 at 9:39 PM, ajax...@gmail.com <ajax...@gmail.com> wrote:
Hello all ,

I am trying to run  a program , but I am getting following error

Traceback (most recent call last):
  File "socket.py", line 28, in <module>
    check = check_server(options.address, options.port)
  File "socket.py", line 8, in check_server
    s = socket.socket()
TypeError: 'module' object is not callable

Looks like your module is also named socket (file is named socket.py). Rename the file to something else and try.
 

--
You received this message because you are subscribed to the Google Groups "Python Pune" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pythonpune+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Vineet Naik

ajax...@gmail.com

unread,
Oct 14, 2013, 2:32:08 PM10/14/13
to pytho...@googlegroups.com
Yes vineet . I renamed the file name. It worked. Thanks.

rahul kumar

unread,
Feb 5, 2019, 12:33:03 AM2/5/19
to Python Pune
This error statement TypeError: 'module' object is not callable is raised as you are being confused about the Class name and Module name. The problem is in the import line . You are importing a module, not a class. This happend because the module name and class name have the same name .

If you have a class "MyClass" in a file called "MyClass.py" , then you should import :
 
from MyClass import MyClass

In Python , a script is a module, whose name is determined by the filename . So when you start out your file MyClass.py with import MyClass you are creating a loop in the module structure.
Reply all
Reply to author
Forward
0 new messages