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.
According to VSCode doc here https://code.visualstudio.com/docs/python/debugging
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
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
}
}
]
}
Launch & Success!

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With