Pycharm python控制台socket.gaierror

时间:2015-08-05 23:15:20

标签: python pycharm

我在OS X Yosemite(10.10.3)上运行Pycharm 4.5.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.

这里提出了一个关于堆栈溢出的类似问题,但错误的根源是传入白色空格的字符串localhost,这不是这里的情况(主机已分配给&#39; localhost&#39;) 。有谁有想法吗?这并不是什么大问题,因为我可以在终端中使用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))

3 个答案:

答案 0 :(得分:7)

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

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

答案 1 :(得分:2)

我是python的新手,半天坚持同样的问题。最后通过将主机设置为127.0.0.1解决了这个问题。 如果您遇到同样的问题,可以通过以下方式执行此操作:

  
      
  1. 在pycharm中打开pydevconsole.py,导航到主脚本部分并找到以下行:   pydevconsole.StartServer(pydev_localhost.get_localhost(), int(port), int(client_port))

  2.   
  3. 按住Ctrl键并单击功能get_localhost()以导航至其来源:

  4.         

    _cache = None def get_localhost():'''

         

    应该在ipv4中返回127.0.0.1,在ipv6中返回:: 1

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”解决了这个问题。

答案 2 :(得分:1)

我在OS X El Capitan上运行了Pycharm 2016.2。

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

如果您要通过Finder导航,请右键单击Pycharm.app文件,然后选择&#34;显示包内容&#34;访问路径。

找到这一行:

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并选择Tools-&gt; Python Console ...

然后你应该看到:

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