Ask Your Question
1

How do you get the linter working for ros2 libraries in visual studio code for a source ros2 installation?

asked 2021-04-18 14:48:32 -0500

rydb gravatar image

updated 2021-04-18 15:16:41 -0500

I'm trying to figure out how to get auto-complete working in vs-code for my source installation of ros2 but position of directories and overall folder structure seems to be different on the source installation from the apt-get install.

E.G:

rlcpy is in /opt/ros/foxy/lib/python3.8/site-packages on the apt-get install method but rlcpy seems to be in (directory you installed it in /ros2_foxy/src/ros2 on the source installation.

launching packages through "ros2 launch <package-name> <package-name-launchfile> works, but when I want to work on a program for my pi nodes in the IDE, the linter fails to import the ros2 libraries because it can't find them, I'm using the settings.json file of:

{
    "python.autoComplete.extraPaths": [
        "/opt/ros/foxy/lib/python3.8/site-packages",
        "~/ros2_foxy/src"
    ],

}

but this does not work.

I've done both source ~/ros2_foxy/install/setup.sh and (directory of ros2 package) source install/setup.sh, but neither of these fixes imports not showing in the linter.

This is the code im trying to run:

#!/usr/bin/env python
import time
import serial
import rclpy
from rclpy.node import Node

from std_msgs.msg import String


class MinimalSubscriber(Node):

    def __init__(self):
        super().__init__('minimal_subscriber')
        self.subscription = self.create_subscription(
            String,
            'topic',
            self.listener_callback,
            10)
        self.subscription  # prevent unused variable warning

    def listener_callback(self, msg):
        self.get_logger().info('I heard: "%s"' % msg.data)


def main(args=None):
    rclpy.init(args=args)

    minimal_subscriber = MinimalSubscriber()

    rclpy.spin(minimal_subscriber)

    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)
    minimal_subscriber.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

Error im getting:

Unable to import 'rclpy.node'

how do i get the vscode workstation working so the linter works?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2022-03-02 00:54:40 -0500

khairulm gravatar image

updated 2022-03-03 23:11:37 -0500

Add the path to rclpy in your linter import paths. For my case it was /home/khairulm/ros2_foxy/install/rclpy/lib/python3.8/site-packages

Edit: For ros2 foxy, if you installed it using the Debian package (apt install ros-foxy-ros-base), the visual studio code linter already knows where to look for rclpy

edit flag offensive delete link more
0

answered 2021-04-22 12:50:44 -0500

RFRIEDM_Trimble gravatar image

updated 2021-04-24 15:33:39 -0500

jayess gravatar image

An import error is not a linter error. That is python not knowing where rcply.node is.

You need to source your ros installation. Depending on your ros install path, you can try the following:

$ source /opt/ros/foxy/setup.bash

Reference: https://docs.ros.org/en/foxy/Tutorial...

What is the command you are using to cause this error in the linter?

edit flag offensive delete link more

Comments

I have sourced my ros2 installation, that's not the issue.

The issue is, I don't know how to get the linter to not show errors, even though the files run just fine with ros2 run or ros2 launch.

rydb gravatar image rydb  ( 2021-04-23 18:31:51 -0500 )edit

The problem isn't sourcing. auto-complete is just looking at the .py files for definitions, at least for the apt-get install version of ROS2. The problem is that the source installation and the apt-get installation have fundamentally different structures for storing their python libraries, so there is no consolidated /site-packages for auto complete to get package definitions from.

I tried setting the path to be the packages inside /install directory post build, but that doesn't work either.

I think auto-complete on the from source installations is just not possible at the moment. At least with Foxy.

rydb gravatar image rydb  ( 2021-04-30 15:16:05 -0500 )edit

Your Answer

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

Add Answer

Question Tools

3 followers

Stats

Asked: 2021-04-18 14:48:32 -0500

Seen: 511 times

Last updated: Mar 03