55

I'm running the visual studio code tutorial with Python and am unable to connect to the debugger. Google / SO are coming up empty when I search. Usually I use Anaconda with Jupyter, so I'm connecting Visual Studio Code to the python in my activated 3.6 virtual environment. I tried pip installing ptvsd in my virtual environment, but that did not make a difference in what I am seeing.

I would welcome any suggestions. Screenshots included below. launch.json in the bottom screenshot

enter image description here enter image description here

bearcat
  • 706
  • 1
  • 7
  • 12
  • I think you have a typo in your `launch.json`; notice the red squiggle? I think you need a comma following the `"console"` line. – Brett Cannon Sep 24 '18 at 19:47
  • 2
    Hi @Brett Cannon, thanks for taking a look at this. I added the comma, which got rid of the squiggle. It has not changed the timeout behavior though, the debug connection is still not made and continues to timeout. – bearcat Sep 26 '18 at 05:33
  • At this point you probably need to open an issue at https://github.com/microsoft/vscode-python to figure out what's going on. Please fill out the issue template when you do and if you can please provide sample code that can reproduce the issue. – Brett Cannon Sep 27 '18 at 19:28
  • 3
    I should have updated this some time ago. I believe this was happening because I was on Conda 2x. I upgraded to Conda 3x and the debugger has worked since. – bearcat May 17 '20 at 23:17

15 Answers15

75

Open the launch.json file and add the following configuration:

{
    "name": "Python: Debug Console",
    "type": "python",
    "request": "launch",
    "program": "${file}",
    "console": "internalConsole"
}

When you specify none internalConsole for the console, it will run the debugger in the debugger console instead of running in the internal or external terminal.

wovano
  • 4,543
  • 5
  • 22
  • 49
Lucas Rath
  • 850
  • 5
  • 4
  • 3
    as of 2022 the "internalConsole" method does not work consistently. – D.L Jun 01 '22 at 17:41
  • @D.L you should expand on that. Just saying doesnt work consistently doesn't say much – barlop Oct 08 '22 at 00:28
  • 1
    this internalConsole setting mght or does have an issue re input https://stackoverflow.com/questions/73993577/in-vs-code-when-running-a-python-program-via-the-debugger-with-internalconsole – barlop Oct 08 '22 at 00:31
14

I had the same issue, so I added the following line to the settings.json file:

{
    // to fix 'Timeout waiting for debugger connections'
    "python.terminal.activateEnvironment": false
}
wovano
  • 4,543
  • 5
  • 22
  • 49
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
13

I had this same problem today. I think it might be a connection problem when the debugger tries to connect to an integrated PowerShell terminal inside Visual Studio. If I change to using an external terminal to run the program then it works okay, and the debugger connects to the external terminal and I can debug perfectly fine. This is my entry for the external terminal launch in launch.json

{
    "name": "Python: Terminal (external)",
    "type": "python",
    "request": "launch",
    "program": "${file}",
    "console": "externalTerminal"
},
wovano
  • 4,543
  • 5
  • 22
  • 49
BigBadMe
  • 1,754
  • 1
  • 19
  • 27
6

I had the same issue. Fixed it by using "integratedTerminal".

PS: My system is Windows 7.

{
    "name": "Debug",
    "type": "python",
    "request": "launch",
    "program": "${workspaceFolder}/main.Py",
    "console": "integratedTerminal"
}
wovano
  • 4,543
  • 5
  • 22
  • 49
Eniru
  • 69
  • 1
  • 1
  • Thanks! Python extension cannot *re*start configuration in the same terminal w/o this line since v2019.09: "Timeout waiting for debugger connection". I've had to close terminal window after every run. (Windows7) – Winand Oct 11 '19 at 05:48
4

Change your integrated terminal shell to cmd.exe. This will allow the default launch.json to debug with the Current File (Integrated Terminal) option.

This change has fixed other bugs in my Visual Studio Code + Python integration, such as the double call to Run Python File in Terminal that was frequently required. It was a nice suggestion coming from an internal dialog in version 1.28.2.

3

I had the same issue today. The reason was that the debug settings were changed to "Current File (Integrated Terminal). Switching back to "Debug Console" fixed the problem for me.

You can find the settings in the drop-down menu right next to the debug button

Marcel
  • 39
  • 3
2

I think this is going to work fine, I tried it:

"configurations": [
    {
        "name": "Python: Debug Console",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal"
    }
]
wovano
  • 4,543
  • 5
  • 22
  • 49
Sandip Swain
  • 417
  • 1
  • 4
  • 10
2

As I see by others' answers, the problem itself may differ from case to case. In my case, I had the gdb package missing. After reinstalling it with sudo apt-get update; sudo apt-get install gdb, I was able to use the debugger.

But if it doesn't help you, here is a nice way to find a reason in your case.

  1. Update the launch.json file and add "logToFile": true flag.
  2. Try running in the debug mode
  3. After failing, look for the details logs by this path: /root/.vscode-server/extensions/ms-python.python-2022.8.0 (it may differ based on your version of the Python VS code extension). The files you're looking for has the .log extension obviously
Yurii
  • 83
  • 2
  • 8
1

I got here using WSL.

I tried to restart a debug session and vs code just dropped back to the prompt. When I ran debug again it timed out as per the OP's error.

Solution was to kill the previous debug session in WSL bash (or vs code bash for that matter), one liner:

sudo kill -9 `ps ax | grep python | grep 5678 | cut -d' ' -f1`

Debug should work again.

Michael Ribbons
  • 1,753
  • 1
  • 16
  • 26
1

Add cmd to your environment variables

That helped me to solve the problem.

You need to add the following path to your system variables path.

%SystemRoot%\system32

Good luck, I hope it solved your problem.

1

Changing your "integratedTerminal" shell to cmd.exe will fix it, because it's using "PowerShell".

  1. Open a new terminal pane : Press Ctrl+` (or View > Terminal in menu)
  2. Open a "Command Prompt" Open a "Command Prompt"
  3. Switch "cmd" to default (DO NOT switch to PowerShell) Switch to default profile
Tong Jiye
  • 26
  • 3
0

I tried all the solutions that have been mentioned here. Nothing worked. Figured out the default timeout in debugpy that VSCode uses is 15 seconds. So if your app doesn't launch within 15 seconds, then VSCode throws this exception.

Solution that works: Create a new environment variable in your user account (not in launch.json) DEBUGPY_PROCESS_SPAWN_TIMEOUT and set it to 90

On Windows:

  1. Search for "Edit environment variables for your account"
  2. Create a new environment variable DEBUGPY_PROCESS_SPAWN_TIMEOUT
  3. Set the value to 90
  4. Close and reopen VSCode
mze3e
  • 438
  • 2
  • 5
  • 14
  • 1
    does not work for me. – Jingnan Jia Feb 08 '23 at 22:02
  • Works for me, thanks! :) I set the value to 180, some sites suggest higher:[`export DEBUGPY_PROCESS_SPAWN_TIMEOUT=500`](https://mila-docs-satyaog.readthedocs.io/_/downloads/en/latest/pdf/). It's important to close and reopen VS Code (as you said) so that VS Code recognizes the environment variable change. "Git Bash" is slow to open on my Windows machine (even without VS Code); I also activate a Python virtualenv. I'm not sure why Git Bash is so slow; maybe my company's antivirus software? Not sure. But if it's slow in general, it will be slow in VS Code, hence increasing the timeout helps. – Nate Anderson Jun 29 '23 at 00:49
0

The highest-voted answer worked for me, but:

  • it only worked after I made sure my program could run in the first place (my program didn't run due to an uncaught exception, so I couldn't debug until I found & fixed that exception)
  • a the highest voted answer is similar to the the advice here, which I think might be better solution for you if you *prefer to see a terminal open...

That is, you can continue to use launch.json "console": "integratedTerminal", but change your default terminal profile to the Command Prompt:

a screenshot showing how to change default terminal to the command prompt, open the terminal tab choose the "down arrow" icon next to the terminal "+" (creation) icon, choose "Select Default Profile", and choose whichever profile you want from the resulting list

(My suggestion is better if you prefer to see Command Prompt output in the opened terminal. But my suggestion is worse if you don't care about Command Prompt output, and/or you don't want to change your default terminal for whatever reason)


EDIT 1: More context

I deleted details (see history of my edits), but in case they help other people find this question & answer, note if you use "Git Bash" as your integrated terminal and you try to debug you may see this error in the terminal after the "Timed out" dialog appears:

More details:
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "c:\Users\username\.vscode\extensions\ms-python.python-2023.8.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher\__main__.py", line 91, in <module>
    main()
  File "c:\Users\username\.vscode\extensions\ms-python.python-2023.8.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher\__main__.py", line 47, in main
    launcher.connect(host, port)
  File "c:\Users\username\.vscode\extensions\ms-python.python-2023.8.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\launcher\__init__.py", line 34, in connect
    raise e
  File "c:\Users\username\.vscode\extensions\ms-python.python-2023.8.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\launcher\__init__.py", line 30, in connect
    sock.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

EDIT 2: Another answer works

This other answer also works for me, without changing my terminal (i.e. still using Git Bash as my default terminal and using "console": "integratedTerminal" in my launch.json

I used a higher value (180) for the DEBUGPY_PROCESS_SPAWN_TIMEOUT environment variable, and I checked echo $DEBUGPY_PROCESS_SPAWN_TIMEOUT actually printed the value.

My "Git Bash" is slow to open in general (even without VS Code). Try opening your Git Bash after a fresh restart of your windows machine; if it takes longer than 15 seconds (default value for DEBUGPY_PROCESS_SPAWN_TIMEOUT), that explains the timeout! (Whereas opening my Command Prompt goes very quickly, which explains why using Command Prompt instead can work!)

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

have you ever changed the default terminal shell of vs code? I ran into the same question after changed the shell of vs code from "cmd" to "msys2". and configure below worked, though I dont know why. "python.terminal.activateEnvironment": false

zhuosheng
  • 11
  • 1
-1

Personnaly I had the issue when having my working directory in a remote location (local network on a NAS). Copying the directory in my machine and working from there solves the issue.

Xema
  • 1,796
  • 1
  • 19
  • 28