Im just beginning learning Python and going through the Python for Everybody course. In Chapter 12, I have hit a roadblock. Whenever I try to Import socket or Import Urlib, I receive a traceback. I receive a traceback no matter what i am trying to import. Please help.
Can you share the exact code that youâre trying? Wrap it in three backticks (```) so that the formating is preserved.
Are you capitalizing the I in âimportâ?
You can also paste the traceback here to let people figure out the issue.
import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('data.pr4e.org', 80))
cmd = 'GET https://data.pr4e.org/intro-short.txt HTTP/1.0\n\n'.encode()
mysock.send(cmd)
while True:
data = mysock.recv(512)
if (len(data) < 1):
break
print(data.decode())
mysock.close()
Try using this cmd
instead:
cmd = 'GET /intro-short.txt HTTP/1.0\r\nHost: data.pr4e.org\r\n\r\n'
It is still giving me an error. It gives several errors but the first traceback error is on the first line.
Oh, I am not capitalizing import. Hold on let me try that.
Traceback (most recent call last):
File âC:\Users\betha\py4e\socket.pyâ, line 1, in
import socket
File âC:\Users\betha\py4e\socket.pyâ, line 3, in
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
^^^^^^^^^^^^^^
AttributeError: partially initialized module âsocketâ has no attribute âAF_INETâ (most likely due to a circular import)
You should avoid naming your file the same as any library youâre importing, and this is something the instructor should emphasize. In your case, avoid naming it socket.py
and use something like sample_1.py
instead.
That is exactly what I have done is name it socket.py and I think that was the problem. Im working to fix it now, thank you.
I worded that poorly, you shouldnât capitalize it, but your first post has it capitalized and I wasnât sure if you had done that in your code.
Its okay. I got the code fixed and its important to remember to make sure i pay attention to capitalization as well as file names. Thanks to everyone for helping.