5

I wanted to debug my local Python code as usual in VS-Code on Windows 10 via pressing F5:

debugging settings VS-Code side-bar

I started having this error already more than a year ago, but recently it became persistent.

The entire error traceback:

$  /usr/bin/env 'DEBUGPY_LOG_DIR=c:\Users\username\.vscode\extensions\ms-python.python-2021.8.1105858891' c:\\Users\\username\\Projects\\project-venv\\Scripts\\python.exe c:\\Users\\username\\.vscode\\extensions\\ms-python.python-2021.8.1105858891\\pythonFiles\\lib\\python\\debugpy\\launcher 56721 -- c:\\Users\\username\\Projects\\project\\test_files\\prediction_performance_monitoring\\modified_app_for_docker_testing.py
Traceback (most recent call last):
  File "C:\Users\username\.pyenv\pyenv-win\versions\3.8.9\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\username\.pyenv\pyenv-win\versions\3.8.9\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\username\.vscode\extensions\ms-python.python-2021.8.1105858891\pythonFiles\lib\python\debugpy\launcher\__main__.py", line 97, in
<module>
    main()
  File "c:\Users\username\.vscode\extensions\ms-python.python-2021.8.1105858891\pythonFiles\lib\python\debugpy\launcher\__main__.py", line 53, in
main
    launcher.connect(host, port)
  File "c:\Users\username\.vscode\extensions\ms-python.python-2021.8.1105858891\pythonFiles\lib\python\debugpy\launcher/../..\debugpy\launcher\__init__.py", line 34, in connect
    sock.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

The .vscode/launch.json debugging configuration for local file testing contains these settings:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "python": "C:\\Users\\andreas.luckert\\Projects\\project-venv\\Scripts\\python.exe",
            "redirectOutput": true,
            "justMyCode": false,
            "logToFile": true,
            "stopOnEntry": false,
        }
    ]
}

Searching for the issue on StackOverFlow there were similar questions, but the answers (similar issue here) mostly stated something akin to

WinError 10061 - means that the server side TCP is not accepting the connection. For there is no application above listening on that port that client is trying to connect. Please check if you have your server application started and that it is listening on the intended port.

Yet, this did not help me because the questions referred to some remote connection, but in my case the debugging process is conducted locally. Moreover, I have not changed anything in the above-mentioned configuration which had been working normally a week ago.

Other answers like here included firewall diffulties, e.g.

Is firewall running on the server? If so, that may be blocking connections. You could disable firewall or add an exception on the server side to allow connections on port 8000.

I could not figure out how my firewall should block this debugging process, especially because it was not a constant issue but came and went irregularly. At times I thought it was related to a temporary shortage of free RAM, but this was proven a false assumption.

By and large, I cannot work sensibly with VS-Code Python debugging anymore. As this is an integral part of my workflow, I need to find out how to get rid of this problem.

Andreas L.
  • 3,239
  • 5
  • 26
  • 65

3 Answers3

4

I needed to set my default VS-Code internal terminal profile to Command Prompt:

screenshot of VS Code showing to open the Terminal, and choose the dropdown arrow to click "Select Default Profile" and change to Command Prompt (cmd)

(Alternatively type F1 (or Ctrl+Shift+P) and type "Terminal: Select Default Profile", type Enter, and up/down-arrow to the Command Prompt choice, then type Enter again, as shown here)

This way, the VS-Code interactive debugger chooses the "cmd"-shell instead of previously "git-bash".

The culprit was the /usr/bin/env in the beginning of the auto-generated python.exe - call, which is prepended only in the git-bash - shell but not in cmd:

$  /usr/bin/env 'DEBUGPY_LOG_DIR=c:\Users\username\.vscode\extensions\ms-python.python-2021.8.1105858891' c:\\Users\\username\\Projects\\project-venv\\Scripts\\python.exe c:\\Users\\username\\.vscode\\extensions\\ms-python.python-2021.8.1105858891\\pythonFiles\\lib\\python\\debugpy\\launcher 56721 -- c:\\Users\\username\\Projects\\project\\test_files\\prediction_performance_monitoring\\modified_app_for_docker_testing.py

This was being blocked by the local firewall for some unknown reason; both on my Ubuntu 20.04 and my Windows 10 machines.

I suppose there is a way to resolve this problem, but for now I don't mind having the Windows-native Command Prompt as the default internal terminal in VS-Code. I can have several terminal types open at the same time and use their different capabilities to my advantage:

enter image description here

In fact, finding this solution was a game changer as beforehand this WinError: 10061 was often preventing me from working effectively with VS-Code.

Nate Anderson
  • 18,334
  • 18
  • 100
  • 135
Andreas L.
  • 3,239
  • 5
  • 26
  • 65
  • Thanks for the answer, I upvoted. Your launch.json says `"console": "integratedTerminal"`, and you had problems when your default terminal is *something other-than Command Prompt*, and you fix the problem when you change default terminal profile to *Command Prompt*. I wonder if you could also fix by simply [changing launch.json to `"console": "internalConsole"`](https://stackoverflow.com/a/52781131/1175496) Maybe equivalent to your fix? I wish the [VS Code documentation would explain the "console" options better](https://code.visualstudio.com/Docs/editor/debugging#_launchjson-attributes) – Nate Anderson Jun 28 '23 at 21:43
  • The other answer I linked above may be comparable to your answer; but your answer is better because the other answer; (use `"console": "internalConsole"`), will **not open a terminal for me to see output** (including the error output you describe here: `[WinError 10061] No connection could be made because the target machine actively refused it`), whereas **your answer will open a terminal and show output**. However, for me, the cmd output is completely empty (there is no error `on sock.connect(...)`, but the program exits and the debugger never opens) -- so I'm still hunting for an answer! – Nate Anderson Jun 28 '23 at 21:50
  • Oops, your answer (and the other answer) work, but there was an exception in my program that prevented it from **running** (forget debugging). Once I could **run** it, then I could **debug** it (with "Command Prompt" terminal, and/or `"console":"internalConsole"`) – Nate Anderson Jun 28 '23 at 23:42
  • OK last comment, [increasing the timeout](https://stackoverflow.com/a/75154592/1175496) helped me too! My Git Bash is slow to open, for some reason -- but my Command Prompt is fast. Your solution is to use Command Prompt instead; an alternative (for me) was to wait **longer** for Git Bash to open! – Nate Anderson Jun 29 '23 at 00:54
1

I don't know why, the Debug Adapter didn't listening on 127.0.0.1, so adding host directive into launch.json like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "host": "127.0.0.1"
        }
    ]
}

it's works for my windows 10 machine.

see also: https://github.com/microsoft/ptvsd/issues/2104

fishtrees
  • 231
  • 3
  • 6
0

If you get this error accompanied by the message "Timeout waiting for debugger connection"... VS Code modal warning when user clicks "debug" button warning says "Timeout waiting for debugger connection"

...it might mean that the VS Code debugging server is waiting for the debugpy debug client, but the debugpy client takes too long because the "integratedTerminal" you're using takes a long time to open (my default terminal, Git Bash, takes a long time to open), or a long time to activate its environment (I use Python virtual environments)...

For me, I could fix it by waiting longer; specifically increasing the DEBUGPY_PROCESS_SPAWN_TIMEOUT value (also mentioned here and here)

To do that in Windows; try the steps below (screenshot follows)

  1. Open "Edit the system Environment Variables" (search the start menu for "Environment Variable")
  2. A "System Properties" dialog opens; click the "Environment Variables" button
  3. Under "User variables" , click the "New" button; give the Variable name: DEBUGPY_PROCESS_SPAWN_TIMEOUT, and the Variable value something high, like 500 (you can always decrease later)
  4. Click "Ok", and "Ok"
  5. Close and re-open Visual Studio Code (so it recognizes the new variable; using "Reload Window" won't work)

a screenshot showing how to add an environment variable DEBUGPY_PROCESS_SPAWN_TIMEOUT with a value of 180 (or higher, like 500) to increase from the default value if no variable specified (15), which might not be enough time for Git Bash to launch , create a debugger client, & connect to the VS Code debugger server

Nate Anderson
  • 18,334
  • 18
  • 100
  • 135