In VSCode, how to debug a python program inside a bash shell script
Asked Answered
A

2

8

I have written a shell script, which will do some configurations first before launching a python program. (e.g., downloading the data, pip install some packages, setting environment variables, etc. Then the heavy work in done inside the python program (e.g., a Deep Learning training task). Now I want to debug the python program, (I'm okay with if I have to debug both the shell and python together). How should I modify the launch.json file to accomplish this? Should I add some task.json items? I'm not familiar with the task topic yet.

Currently, I am doing this in a ugly workaround way. I commentted out the python train.py statement in the shell script and run the shell script first. Then run the python scipt python train.py alone in debug mode. I think this currently works in my simple situation, but appearenlyt if inside the shell script, we are doing something like temporarily modify the environment variables, etc. I can't separate these two steps. So I want to know if there is a more straightforward and decent way. Much appreciated.

Alunite answered 28/7, 2021 at 13:39 Comment(0)
V
1

According to VSCode doc here https://code.visualstudio.com/docs/python/debugging

Step 1

Install debugpy to your env, and add "wait for client" code in your entrypoint python script.

pip install debugpy

import debugpy
debugpy.listen(5678)  # 5678 is port
print("Waiting for debugger attach")
debugpy.wait_for_client()
debugpy.breakpoint()
print('break on this line')

Then launch your bash sciprt from a terminal (with adim or sudo permission, otherwise, potential socket permission error), and wait for waiting message to be shown.

Or add this to your python arg

python -m debugpy --listen 5678 --wait-for-client XXX.py

Step 2

Add "Attch to python" config to your VSCode debug launch.json like below

{
    // 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",
            "type": "python",
            "request": "attach",
            "connect": {
              "host": "127.0.0.1", // replace this with remote machine name
              "port": 5678
            }
        }
    ]
}

Step 3

Launch & Success! enter image description here

Vernita answered 27/11, 2023 at 14:14 Comment(1)
You forgot the critical step: Once the Bash code is launched and launches the python code, it stops at wait_for_client(). Then, you use the VS debugger dropdown to select the Python Attach (debug config), and press Play/F5. Then, it will attach and start debugging your code.Installment
G
0

If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.

Grit answered 3/12, 2023 at 10:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.