Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vscode python debugger not working: connection refused

Whenever I try to debug python code, vscode gives an errror and informs me the connection of the debugger was refused. I attached the full stack trace at the bottom

I'm on CentOS8 Linux.

So far I've tried:

  • SELinux is off
  • firewall is disabled
  • uninstall vscode
  • remove /home/user/.vscode
  • remove /home/user/.config/Code
  • clear environment variables

Not really an idea about what might be interfering here.

The file I'm debuggin is print('test') saved as test.py :-)

/usr/bin/python /home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 43259 /scratch/user/tmp/test.py 
E00000.119: Exception escaped from start_client

            Traceback (most recent call last):
              File "/home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/lib/python/old_ptvsd/ptvsd/log.py", line 110, in g
                return f(*args, **kwargs)
              File "/home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/lib/python/old_ptvsd/ptvsd/pydevd_hooks.py", line 74, in start_client
                sock, start_session = daemon.start_client((host, port))
              File "/home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/lib/python/old_ptvsd/ptvsd/daemon.py", line 217, in start_client
                connect(client, addr)
              File "/home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/lib/python/old_ptvsd/ptvsd/socket.py", line 197, in connect
                sock.connect(addr)
            ConnectionRefusedError: [Errno 111] Connection refused


Traceback (most recent call last):
  File "/home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/ptvsd_launcher.py", line 43, in <module>
    main(ptvsdArgs)
  File "/home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/lib/python/old_ptvsd/ptvsd/__main__.py", line 432, in main
    run()
  File "/home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/lib/python/old_ptvsd/ptvsd/__main__.py", line 300, in run_file
    setup_connection()
  File "/home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/lib/python/old_ptvsd/ptvsd/__main__.py", line 291, in setup_connection
    daemon = ptvsd._remote.attach(addr)
  File "/home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/lib/python/old_ptvsd/ptvsd/_remote.py", line 95, in attach
    patch_multiprocessing=ptvsd.options.multiprocess)
  File "/home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/lib/python/old_ptvsd/ptvsd/_vendored/pydevd/pydevd.py", line 2179, in settrace
    wait_for_ready_to_run,
  File "/home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/lib/python/old_ptvsd/ptvsd/_vendored/pydevd/pydevd.py", line 2230, in _locked_settrace
    debugger.connect(host, port)  # Note: connect can raise error.
  File "/home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/lib/python/old_ptvsd/ptvsd/_vendored/pydevd/pydevd.py", line 1060, in connect
    s = start_client(host, port)
  File "/home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/lib/python/old_ptvsd/ptvsd/pydevd_hooks.py", line 136, in _start_client
    return start_client(daemon, h, p)
  File "/home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/lib/python/old_ptvsd/ptvsd/log.py", line 110, in g
    return f(*args, **kwargs)
  File "/home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/lib/python/old_ptvsd/ptvsd/pydevd_hooks.py", line 74, in start_client
    sock, start_session = daemon.start_client((host, port))
  File "/home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/lib/python/old_ptvsd/ptvsd/daemon.py", line 217, in start_client
    connect(client, addr)
  File "/home/user/.vscode/extensions/ms-python.python-2020.1.58038/pythonFiles/lib/python/old_ptvsd/ptvsd/socket.py", line 197, in connect
    sock.connect(addr)
ConnectionRefusedError: [Errno 111] Connection refused
dmaarten
like image 755
maartend Avatar asked Jun 27 '26 14:06

maartend


1 Answers

I have the same problem.After a long time of research, I found the reason for my problem:

  1. The localhost in my /etc/hosts file is not 127.0.0.1
  2. Therefore, DebugAdapter is not listening on the address 127.0.0.1
  3. Therefore, "ptvsd --client --host localhost" cannot connect to DebugAdapter

Solutions:

define host as 127.0.0.1 in lanuch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "host": "127.0.0.1"
        }
    ]
}
like image 119
Andy Zhong Avatar answered Jun 30 '26 05:06

Andy Zhong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!