Debugging Python Scripts Locally Using VS Code and Debugpy

You can do this by opening the Extensions view (Ctrl+Shift+X) and searching for “Python” and “Debugpy”. Make sure both extensions are installed and enabled.

2. Create a new launch configuration file named `launch.json` in your project folder or open an existing one if you have one. You can do this by clicking on the Run and Debug icon (the green play button with a bug) at the top of Visual Studio Code, then selecting “Add Configuration…” from the dropdown menu that appears.

3. In `launch.json`, add the following configuration:

{
  "name": "Python: Flask (remote)", // This is the name of the configuration, used for identification purposes.
  "type": "debugpy", // This specifies the type of debugger being used.
  "request": "attach", // This specifies the type of request being made to the debugger.
  "host": "<your-server-ip>", // This is the IP address of the server being connected to.
  "port": <your-server-port> // This is the port number of the server being connected to.
}

Replace `` and `` with the IP address of your server and the port number that Debugpy is listening on. You can find this information by running `python -m debugpy –listen ` in a terminal or command prompt window on your server.

4. Save the changes to `launch.json`.

5. Run the launch configuration you just created by clicking on the green play button with a bug at the top of Visual Studio Code, then selecting “Python: Flask (remote)” from the dropdown menu that appears. This will attach Debugpy to your running Python script on the remote server and allow you to debug it in Visual Studio Code.

6. Set breakpoints in your code by clicking on the left gutter next to the line numbers where you want them, or by pressing F9. You can also use conditional breakpoints (e.g., “hit count > 5”) if needed.

7. Run your Python script on the remote server using a tool like `python manage.py runserver` for Flask applications, and it will automatically connect to Debugpy in Visual Studio Code. You can then step through your code line by line, inspect variables, and use other debugging features as needed.

8. When you’re done debugging, press Ctrl+Shift+D (Windows/Linux) or Command+Shift+D (MacOS) to detach Debugpy from the remote server.

debug_this_thread()` at the top of each thread that you want to debug. This will ensure that Debugpy can attach to those threads and allow you to step through them as needed.

For more information on remote debugging with VS Code, see the Remote Debugging documentation in Visual Studio Code’s official documentation.

SICORPS