I'm using an Arch Linux machine and trying to run the following code from a Python file.
import socket
import sys
if __name__ == "__main__":
print(sys.platform)
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
and it keeps telling me that
AttributeError: module 'socket' has no attribute 'AF_UNIX'
sys.platform
prints linux
socket.AF_INET
has the same issuepython -c "import socket; socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
returns the same errorThe error is caused by an unlucky choice of variable name. The variable socket
shadows the module of the same name.
The first time the line is run, it works fine:
import socket
socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# works fine, but now socket points to the object returned by the socket.socket() call
socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# error!
Solution: use a different variable name, e.g. sock
:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)