Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: name 'AF_INET' is not defined (ONLY ON MAC)

Tags:

python

sockets

So I have python installed in three places: A Windows machine, a Pi (Running Kali), and my Mac. My Mac is the only one where I get this error when either importing the socket library or a library dependent upon the socket library (i.e. ipwhois). I ran it in sudo, I tried both python 2 and 3 and still no success. Here is what I get:

Python 2.7.14 (default, Oct  1 2017, 15:09:23) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>>import socket
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "socket.py", line 15, in <module>
    serverSocket = socket(AF_INET, SOCK_STREAM)
NameError: name 'AF_INET' is not defined
>>> import ipwhois
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/user/homebrew/lib/python2.7/site-packages/ipwhois/__init__.py", line 26, in <module>
    from .net import Net
  File "/Users/user/homebrew/lib/python2.7/site-packages/ipwhois/net.py", line 26, in <module>
    import socket
  File "socket.py", line 15, in <module>
    serverSocket = socket(AF_INET, SOCK_STREAM)
NameError: name 'AF_INET' is not defined

Again, no issues with either of these libraries on my Windows or Linux box. I'm not really sure where to go from here. Any help is appreciated.

like image 277
Winner1235813213455 Avatar asked Oct 27 '25 05:10

Winner1235813213455


2 Answers

AF_INET is define inside the socket, so try the below way:

from socket import AF_INET, SOCK_STREAM

If you want to know what it include inside the socket try the follow way:

import socket

print dir(socket)
like image 87
Frank AK Avatar answered Oct 28 '25 19:10

Frank AK


From your comment, the error occurs in one specific directory. The socket module tries to import some other modules, notably _socket. If you have a file with this name in current directory, bad things are guaranteed to occur. The general rule is that you should never have a user module starting with a _ or re-using the name of a module from the standard library.

like image 43
Serge Ballesta Avatar answered Oct 28 '25 18:10

Serge Ballesta



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!