Ask Your Question

ROS Debugging VSCode Python

asked 2018-08-20 08:31:43 -0500

JeffBlumenthal gravatar image

updated 2018-08-20 08:58:28 -0500

gvdhoorn gravatar image

Hi Everyone,

I have read/tried various solutions in an attempt to debug ROS in VS Code without any luck. Can I please get some help in solving this mystery?

My workspace runs fine from when I run from an Ubuntu terminal prompt and from an IDE terminal prompt. source devel/setup.bash rosrun location

My env: Ubuntu 16.04 VS Code IDE ROS extension installed

My ROS package: Name: location Simple pub/sub with a custom message

From an Ubuntu terminal prompt I go to my workspace root, and start code. In VS Code terminal (which is on my workspace root), run catkin build then roscore. In the lower left corner of IDE, ROS Master now has a check mark.

I then created a launch configuration by:

  1. clicking on the debug icon on the left most part of the IDE
  2. clicking on the gear icon on the upper left section of the IDE, this opens a new launch.json file
  3. click on the the button Add Configuration...(in the lower right of the window). This pulls up a window of choices
  4. choose {} ROS : rosrun
  5. Here is my launch entry:

      "request": "launch",
      "type": "ros",
      "command": "rosrun",
      "name": "rosrun",
      "package": "location",
      "target": "",
      "debugSettings": "debugSettings"
  6. Click on my source file file tag in the editer window

  7. Press F5

The debug process starts and get this error: Exception has occurred: str

'No module named rospy'
  File "/home/zzfl0b/location/src/location/scripts/", line 3, in <module>
  File "/usr/lib/python2.7/", line 72, in _run_code
  File "/usr/lib/python2.7/", line 174, in _run_module_as_main

In IDE terminal pip list | grep ros returns:

gazebo-ros                    2.5.17
rosbag                        1.12.13
rosboost-cfg                  1.14.3
rosclean                      1.14.3
roscreate                     1.14.3
rosgraph                      1.12.13
roslaunch                     1.12.13
roslib                        1.14.3
roslint                       0.11.0
roslz4                        1.12.13
rosmake                       1.14.3
rosmaster                     1.12.13
rosmsg                        1.12.13
rosnode                       1.12.13
rosparam                      1.12.13
rospy                         1.12.13
rosservice                    1.12.13
rostest                       1.12.13
rostopic                      1.12.13
rosunit                       1.14.3
roswtf                        1.12.13
smach-ros                     2.0.1
tf2-ros                       0.5.17

Also from an IDE terminal prompt,

$ env | grep ros:
PKG_CONFIG_PATH=/home/jeff/location/devel/lib/pkgconfig:/opt/ros ...
edit retag flag offensive close merge delete


you need to setup your environment for debugging and source that path, then run your package and link it from VSCode

Choco93 gravatar image Choco93  ( 2018-08-21 02:55:59 -0500 )edit

@Choco93, can you please be more specific on how to do this? I have also tried running source devel/setup.bash from a launch pretask and it still is not working. Thank you.

JeffBlumenthal gravatar image JeffBlumenthal  ( 2018-08-21 10:09:40 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2018-08-23 02:10:43 -0500

Choco93 gravatar image

updated 2018-08-24 01:15:23 -0500

There might be other ways but this is one I know:

  1. Go into your workspace and show hidden folders (ctrl+h)
  2. There is folder called .catkin_tools, and a sub folder called profiles. Go into profiles and make a copy of your default profile. Call it debug (or anything you like). Go into debug and change build.yaml and config.yaml to not to have build and devel but to have debug_devel and build_devel (again you can name them anything).build_space: build_debug & devel_space: devel_debug.
  3. Then build your package in debug mode add a flag when building CMAKE_BUILD_TYPE=debug.
  4. When it's built, source devel_debug/setup.bash and run your node.
  5. Then go into VSCode and debug mode (ctrl+shift+D), you will see green play button on top right press that, it will probably prompt to launch a json, open it and configure it for your python node see here.
  6. Once configured you can chose that configuration and press play button it will pop-up a list of available process to link against, select your node from it, it will ask for password and then you can debug.

It seems lengthy but most of it is just one time configuration thing, once that is done you can change node name and path in configuration to work with any other python node. Hope this helps.

edit flag offensive delete link more


Thank you for your response/help.

So step 4 runs the node and step 6 attaches to it? In the launch.json I have program set to ${workspaceFolder}/src/location/scripts/

It starts ok, then errors out on import rospy with module not found error.

JeffBlumenthal gravatar image JeffBlumenthal  ( 2018-08-23 13:13:42 -0500 )edit

Welcome, 4 and 6 work exactly as you mentioned. And I don't know where this error is coming from, don't remember encountering this. Maybe you can ask a new question about it in more details and close this one.

Choco93 gravatar image Choco93  ( 2018-08-24 01:20:25 -0500 )edit

answered 2021-10-03 11:14:06 -0500

Krishna7 gravatar image

Assume the file we want to run using the ROS launch file is Make it executable. The contents of the file should be following:

import debugpy

import os

a = 34
print("Dexter Rocks")

It would be best if you had the below snippet always at the top:

import debugpy


This snippet will pause the execution further and wait for VSCode to connect. Once the VSCode connects, it starts the execution. Now have the below configuration for your VSCode's launch.json

    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit:
    "version": "0.2.0",
    "configurations": [
            "name": "Python: Current File",
            "type": "python",
            "request": "attach",
            "program": "${file}",
            "connect": {
                "host": "localhost",
                "port": 5678

Remember to have port numbers the same (5678 in our case). That's it. Now run your python file in ROS launch file, and the python execution pauses and waits for VSCode to connect. Once the VSCode connects, it will automatically stop at the debug point set in the VSCode.

This method is useful in general not only with ROS but for every other method where the python file gets executed by some other process and you want to connect to the process and start debugging it. Hope it helps. Thank you.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-08-20 08:31:43 -0500

Seen: 4,791 times

Last updated: Aug 24 '18