При построении маленькой программы с использованием библиотеки socket (Python 3.9) столкнулся с проблемой: PyCharm 2020.3 не видит атрибут AF_UNIX обозначенной библиотеки. Собственно, код:
import os
import socket
unix_socket_name = 'unix.sock'
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
if os.path.exists(unix_socket_name):
os.remove(unix_socket_name)
sock.bind(unix_socket_name)
while True:
try:
result = sock.recv(1024)
except KeyboardInterrupt:
sock.close()
break
else:
print('Message:', result.decode('utf-8'))
Дебаг (build 203.6682.179) выдает ошибку:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.3.2\plugins\python-ce\helpers\pydev\pydevd.py", line 1477, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.3.2\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/panse/PycharmProjects/socket/socket_UNIX_server.py", line 5, in <module>
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
AttributeError: module 'socket' has no attribute 'AF_UNIX'
Однако, в всплывающем вспомогательном окне он присутствует:
И также я его нашел в скрипте стокового модуля:
def socketpair(family=None, type=SOCK_STREAM, proto=0):
"""socketpair([family[, type[, proto]]]) -> (socket object, socket object)
Create a pair of socket objects from the sockets returned by the platform
socketpair() function.
The arguments are the same as for socket() except the default family is
AF_UNIX if defined on the platform; otherwise, the default is AF_INET.
"""
if family is None:
try:
family = AF_UNIX
except NameError:
family = AF_INET
a, b = _socket.socketpair(family, type, proto)
a = socket(family, type, proto, a.detach())
b = socket(family, type, proto, b.detach())
return a, b
"Вошь ли я? Тварь ли я дрожащая?" или проблема в PyCharm? Заранее спасибо P.S. В Sublime Text 3 такая же история