If your launch script allows you to specify a python target (and finishes quickly enough), you can use pythonArgs in launch.json to insert a shim.
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch via Shell Script",
"type": "python",
"pythonArgs": ["debug_shim.py", "launch.sh"],
"args": ["your", "app", "args"],
"request": "launch",
"program": "app.py"
}
]
}
debug_shim.py
import os, subprocess, sys
launcher, debugger, debugger_args = sys.argv[1], sys.argv[2], sys.argv[3:]
sys.exit(subprocess.run(
args=[launcher] + debugger_args,
env=dict(os.environ, LAUNCH_TARGET=debugger),
stdin=sys.stdin,
stdout=sys.stdout,
stderr=sys.stderr,
).returncode)
launch.sh
#!/usr/bin/env bash
# Or target could be passed along the command line as well.
# LAUNCH_TARGET="${1?}"
# ...stuff...
python "${LAUNCH_TARGET-app.py}" "${@}"
In my setup of v1.80.0 on WSL2, the debugger call is like so:
python python_args... debugger debugger_args... app app_args...
Then the call stack will be vscode -> shim -> launcher -> debugger -> app and the debugger will connect seamlessly, although if it takes more than several seconds VSCode will time out.
Debugging an active python file that creates launch.json is debugging launch.json
How do I run a Python script on a set of JSON files? - Stack Overflow
Execute a command in the same shell before debugging
tasks.json to execute Python script failing
Videos
If your launch script allows you to specify a python target (and finishes quickly enough), you can use pythonArgs in launch.json to insert a shim.
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch via Shell Script",
"type": "python",
"pythonArgs": ["debug_shim.py", "launch.sh"],
"args": ["your", "app", "args"],
"request": "launch",
"program": "app.py"
}
]
}
debug_shim.py
import os, subprocess, sys
launcher, debugger, debugger_args = sys.argv[1], sys.argv[2], sys.argv[3:]
sys.exit(subprocess.run(
args=[launcher] + debugger_args,
env=dict(os.environ, LAUNCH_TARGET=debugger),
stdin=sys.stdin,
stdout=sys.stdout,
stderr=sys.stderr,
).returncode)
launch.sh
#!/usr/bin/env bash
# Or target could be passed along the command line as well.
# LAUNCH_TARGET="${1?}"
# ...stuff...
python "${LAUNCH_TARGET-app.py}" "${@}"
In my setup of v1.80.0 on WSL2, the debugger call is like so:
python python_args... debugger debugger_args... app app_args...
Then the call stack will be vscode -> shim -> launcher -> debugger -> app and the debugger will connect seamlessly, although if it takes more than several seconds VSCode will time out.
Adding the setup scripts to your .bashrc file will ensure that they are sourced before vscode begins debugging your program. The ROS2 documentation supports this method and provides an example.
In your .bashrc file add the following:
source /opt/ros/${ROS_DISTRO}/setup.bash
source ~/ros2_ws/install/local_setup.bash
Hey everyone,
I want to debug a Python Skript using a different arch (x86_64 instead of arm64) then my default terminal. I was looking for ways to define a terminal profile (see also on stackoverflow) using the launch.json, but I haven't found a way yet. I started trying to use the argument preLaunchTask, but this seems to be executed outside of the terminal (and it also freezes for this specific task). Any ideas how to fix this?
// launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python (x86_64): script.py",
"type": "python",
"request": "launch",
"program": "/path/to/script.py",
"console": "integratedTerminal",
"justMyCode": true,
"preLaunchTask": "x86_64"
}
]
}
//tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "x86_64",
"type": "shell",
"command": "arch -x86_64 zsh"
}
]
}Specify the module you want to run with "module": "torch.distributed.launch"
You can ignore the -m flag. Put everything else under the args key.
Note: Make sure to include --nproc_per_node and the name of file (main_swav.py) in the list of arguments
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "debugpy",
"module": "torch.distributed.launch",
"request": "launch",
"console": "integratedTerminal",
"args": [
"--nproc_per_node", "1",
"main_swav.py",
"--data_path", "/dataset/imagenet/train",
]
}
]
}
Read more here: https://code.visualstudio.com/docs/python/debugging#_module
This is an example of my launch.json that I use to debug Python modules.
It has an additional configuration to debug "current file" (not as module) which is useful to keep.
{
linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "path.to.module",
"args": ["run_example --arg <arg>"],
"justMyCode": true
},
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
}
]
}
This would replicate a terminal command to run a Python module like so:
python -m path.to.module run_example --arg <arg>
Hello guys,
I am debugging djnago using vscode. What i want to do is to activate a virtual environment before the debugger starts so that it can get the environment to run. So is rhere any launch configuration to achieve this?
I think the --City and Auckland are used as a single argument. Maybe try separating them like so...
Single argument
"args": ["--city","Auckland"]
Multiple arguments and multiple values
Such as:
--key1 value1 value2 --key2 value3 value4
Just put them into the args list one by one in sequence:
"args": ["--key1", "value1", "value2", "--key2", "value3", "value4"]
I also noticed that if you run the script by clicking on the debug button that looks like this
, then the arguments are not passed. However, using Run -> Start Debugging (or its shortcut F5) passed the arguments successfully.
Hello everyone, Vscode noob here.
I have a python script that takes in command line arguments to run, and a bash script that calls the python script with the desired arguments. Now I'm trying to debug the python script with different arguments. I know how to create a launch.json to debug the python part, but AFAIK that involves copying my bash script and reformatting it to .json standards, and since the number of arguments is quite high that takes a while.
So my question is: can I forgo the launch.json directly and debug from the bash script? Or create a launch.json for the bash script (no arguments required for that one) in order to debug the python script? Or is there another way I should be doing this entirely?