【问题标题】:Pycharm python console socket.gaierrorPycharm python控制台socket.gaierror
【发布时间】:2016-12-27 20:35:43
【问题描述】:

我在 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 传入,这里不是这种情况(主机已分配给“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))

【问题讨论】:

  • “127.0.0.1”有效吗?
  • 您能否也发布引发此异常的程序的相关部分。
  • @Totem 这是用于启动pydev控制台的内部pycharm脚本,所以无法更改内部代码。我在 main 中查看了他们的源代码,并将其发布在 main 代码框中。
  • @HaleemurAli 这就是与该程序相关的所有内容。只需单击 PyCharm 工具栏,即可抛出此问题。我自己没有编写任何代码来实现这一点。

标签: python pycharm


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我是python新手,在同一个问题上卡了半天。最后通过设置主机为127.0.0.1解决了这个问题。 如果你有同样的问题,你可以这样做:

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

    2. ctrl + 点击函数get_localhost() 导航到它的源:

    _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”即可解决问题。

    【讨论】:

      【解决方案3】:

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

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

      如果您通过 Finder 导航,请右键单击 Pycharm.app 文件并选择“显示包内容”以访问路径。

      找线:

      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->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
      

      【讨论】:

        猜你喜欢
        • 2015-06-17
        • 2017-09-08
        • 2018-04-01
        • 2015-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-17
        相关资源
        最近更新 更多