Search code examples
pythonvisual-studio-codevscode-debuggerblessed

How to attach VSCode debugger to Python application already running in Windows terminal?


Background

I have a pet project game written in Python which uses blessed to draw graphics to a Windows command prompt terminal window.

To help develop this, I'd like to be able to make use of the debugging features of an IDE with breakpoints, step-into etc. However, this isn't super straightforward as the graphics do not behave well when the script is executed in the built-in terminal of an IDE (I've specifically tried Thonny and VSCode).

Problem Encountered:

I'm trying to take the following alternative approach with VSCode:

  • Launch a Windows command prompt window, and within this navigate to my project directory, activate a virtual environment and start the game with python game.py
  • In VSCode now (with the Python interpreter having already been set to that from my virtual environment used to run the game), set a breakpoint somewhere just into the game loop of game.py
  • go to Run & Debug, and select Python: Attach using Process Id
  • Click Start debugging. This opens a dropdown to select the process
  • From the dropdown, search "game" and select the one which says python game.py

What I expect to happen:

  • The debugger will attach to the python process running my game in the command prompt window, and the debugger code view in VSCode will probably jump to my set breakpoint or something and allow me to inspect variables, step things forward...

What actually happens:

  • In the command prompt window running the game, Unable to find python module. is printed at the current cursor location
  • VSCode thinks for maybe 10 seconds and eventually opens an error window which says Timed out waiting for debug server to connect.

My question is:

  1. Can this approach be made to work? If so, am I doing something wrong and how can I correct this?
  2. Is there some alternative approach which might be more suitable for my scenario?

Extra information

Here is the launch.json debugging configuration created by VSCode:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Attach using Process Id",
            "type": "python",
            "request": "attach",
            "processId": "${command:pickProcess}",
            "justMyCode": true
        }
    ]
}

Python version 3.11.3

Description of virtual environment installed packages using output from pipenv graph:

blessed==1.20.0
  - jinxed [required: >=1.1.0, installed: 1.2.0]
    - ansicon [required: Any, installed: 1.89.0]
  - six [required: >=1.9.0, installed: 1.16.0]
  - wcwidth [required: >=0.1.4, installed: 0.2.8]
opencv-python==4.8.1.78
  - numpy [required: >=1.17.0, installed: 1.26.1]
  - numpy [required: >=1.17.3, installed: 1.26.1]
  - numpy [required: >=1.23.5, installed: 1.26.1]
  - numpy [required: >=1.21.2, installed: 1.26.1]
  - numpy [required: >=1.19.3, installed: 1.26.1]

Solution

  • If anyone has a similar issue, I was able to achieve the desired outcome by using a different configuration:

    {
        "name": "Python: Current File (External Terminal)",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "externalTerminal"
    }
    

    With this configuration, I can simply click start debugging in VSCode and it will open a fresh external Windows command prompt for me running the game, with debugging in VSCode!