Pycharm python 控制台 socket.gaierror

Pycharm python console socket.gaierror

我是 运行 Pycharm 4.5.3 OS X Yosemite (10.10.3)。我创建了一个简单的 python 程序,并尝试打开 python 控制台,但得到了这个堆栈跟踪错误:

/usr/bin/python -u /Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py 59286 59287
Error starting server with host: localhost, port: 59286, client_port: 59287
Unhandled exception in thread started by <function start_server at 0x100d9bd70>
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py", line 283, in start_server
    server = XMLRPCServer((host, port), logRequests=False, allow_none=True)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SimpleXMLRPCServer.py", line 593, in __init__
    SocketServer.TCPServer.__init__(self, addr, requestHandler, bind_and_activate)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 419, in __init__
    self.server_bind()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 430, in server_bind
    self.socket.bind(self.server_address)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
Couldn't connect to console process.

这里也提过stack overflow的类似问题,但是错误的根源是传入的字符串localhost有空格,这里不是这样的(host已经分配给'localhost')。有人有什么主意吗?这没什么大不了的,因为我可以在终端中使用 python 命令行,但我很好奇这是否是 Pycharm.

中的错误

编辑:这是 Pycharm 脚本的源代码。

if __name__ == '__main__':
    import pydevconsole
    sys.stdin = pydevconsole.BaseStdIn()
    port, client_port = sys.argv[1:3]
    import pydev_localhost

    if int(port) == 0 and int(client_port) == 0:
        (h, p) = pydev_localhost.get_socket_name()

        client_port = p

    pydevconsole.StartServer(pydev_localhost.get_localhost(), int(port), int(client_port))

我是 python 的新手,在同一个问题上卡了半天。最后通过将主机设置为 127.0.0.1 解决了这个问题。 如果您遇到同样的问题,可以通过以下方式解决:

  1. open pydevconsole.py in pycharm, navigate to the main script part and find this line: pydevconsole.StartServer(pydev_localhost.get_localhost(), int(port), int(client_port))

  2. ctrl + click the function get_localhost() to navigate to its source:

_cache = None def get_localhost():'''

Should return 127.0.0.1 in ipv4 and ::1 in ipv6

localhost is not used because on windows vista/windows 7, there can be issues where the resolving doesn't work
properly and takes a lot of time (had this issue on the pyunit server).

Using the IP directly solves the problem.
'''
#TODO: Needs better investigation!

global _cache
if _cache is None:
    try:
        for addr_info in socket.getaddrinfo("localhost", 80, 0, 0, socket.SOL_TCP):
            config = addr_info[4]
            if config[0] == '127.0.0.1':
                _cache = '127.0.0.1'
                return _cache
    except:
        #Ok, some versions of Python don't have getaddrinfo or SOL_TCP... Just consider it 127.0.0.1 in this case.
        _cache = '127.0.0.1'
    else:
        _cache = 'localhost'

return _cache

我认为问题是由于此函数返回“localhost”引起的,将其设置为“127.0.0.1”解决了问题。

我在 OS X El Capitan 上遇到了这个问题,运行 Pycharm 2016.2.

在任何编辑器中打开:/Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py

如果您通过 Finder 导航,请右键单击 Pycharm.app 文件并选择 "Show Package Contents" 以访问该路径。

找到行:

pydevconsole.start_server(pydev_localhost.get_localhost(), int(port), int(client_port))

改为:

pydevconsole.start_server('127.0.0.1', int(port), int(client_port))

重新启动 Pycharm 并选择工具->Python 控制台...

然后您应该会看到:

Users/.../env/bin/python /Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py 56582 56583
PyDev console: starting.


import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['/Users/...'])

Python 3.5.0 (default, Dec  1 2015, 12:50:23) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.1.76)] on darwin

到目前为止给出的答案没有找到根本原因,这很可能是您的 OS X /etc/hosts 文件不包含以下条目:127.0.0.1 localhost

使用以下行更新主机文件: 127.0.0.1 localhost (您将需要使用 sudo),然后重新启动 pyCharm。除非您知道自己在做什么,否则编辑 IDE 源代码不是一个好主意。