AttributeError module 'socket' has no attribute 'socket'

import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('data.pr4e.org', 80))
cmd = 'GET http://data.pr4e.org/intro-short.txt HTTP/1.0\r\n\r\n'.encode()
mysock.send(cmd)

while True:
    data = mysock.recv(512)
    if len(data) < 1:
        break
    print(data.decode(),end='')

mysock.close()

Hi,

I just started learning python through Coursera. So far it’s good but with ‘socket’ I keep getting attribute error. Can anybody help me on this one?
What am i doing wrong?

Thank you for any reply.

Traceback (most recent call last):
File “C:\Users*********************************”, line 5, in
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
^^^^^^^^^^^^^
AttributeError: module ‘socket’ has no attribute ‘socket’

My first guess is that you named a file socket.py in the same directory, and you’re importing that module instead of the built-in one?

When you run a python script with python myscript.py, it will add the local directory to the path, and look for modules there before it looks in the standard library. So it’s easy to mess up and mask a standard module with your own.

1 Like

Thank you so much! I had a file named socket.py. After deleted that file, it worked!

1 Like