Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: module 'socket' has no attribute 'AF_INET'

Tags:

python

sockets

the code was working fine, now it shows the error

AttributeError: module 'socket' has no attribute 'AF_INET'

The code as below: Module name is not socket.py

import socket
client= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host='192.168.xx.x'
port=4196
client.connect((host, port))
print("connected to IP", host,"and port", port) 

Error:

`Traceback (most recent call last):
File "C:\Users\User\eclipse-workspace\New\soc.py", line 1, in <module>
   import socket
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 2, in <module>
    client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
AttributeError: module 'socket' has no attribute 'AF_INET'

`

like image 941
kn_pro Avatar asked Jan 29 '26 13:01

kn_pro


1 Answers

You're likely to have a module named socket.py in your project directory, in which case it will take precedence over the standard module when you do import socket. Rename the socket.py in your project directory to something else and your code would work.

like image 57
blhsing Avatar answered Jan 31 '26 02:01

blhsing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!