ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

vscode: rospy import error

asked 2021-07-19 01:51:38 -0500

krushnal gravatar image

updated 2021-09-20 02:23:29 -0500

gvdhoorn gravatar image

The env I am using is ROS Noetic and VS Code along with Pylance.

  1. launching rospy nodes is working fine
  2. importing rospy gives a squiggle error in VSCode.
  3. Also, python syntax is not being highlighted properly. Along with that, I am also not getting auto-complete options.

I think it should be a configuration issue, but I don't know where and what I have to configure to resolve this. Any help would be appreciated.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
6

answered 2021-07-19 06:47:27 -0500

Ranjit Kathiriya gravatar image

updated 2021-09-17 03:38:05 -0500

Hello there,

You have to add 2 python paths into your settings.json file into VS Code.

These are the paths: The first path is for all packages of your local workspace and the second is for the global package.

{
    "python.autoComplete.extraPaths": [
        "/home/<pc-name>/<ws-name>/devel/lib/python3/dist-packages",
        "/opt/ros/noetic/lib/python3/dist-packages"
    ]
}

If you are using CPP then you have to add the path in the c_cpp_properties.json file.

The location of setting and c_cpp_properties JSON files will be :

 "~/<work-space>/devel/include"

Just to note for Pycharm Users: if you are using Pycharm then give this both paths by following these steps:

  1. Go to settings.
  2. Project: src > Project Structure > On the right-hand side click on + Add Content Root and locate both paths.

Try this you will be able to solve your problem and if you have any issues feel free to drop a comment.

edit flag offensive delete link more

Comments

1

thanks a lot .... that solves it!!!

krushnal gravatar image krushnal  ( 2021-07-19 07:02:46 -0500 )edit
0

answered 2023-07-21 08:23:11 -0500

infraviored gravatar image

Even with this guide, i still can not figure it out.

I have a workspace called happyspace. The path to the dist-packages is

/home/flo/happyspace/devel/lib/python3/dist-packages

so I added this to my workspace config:

{
    "folders": [
        {
            "path": "."
        }
    ],
    "settings": {
        "editor.fontSize": 15,
        "[python]": {
            "editor.defaultFormatter": "ms-python.autopep8"
        },
        "python.formatting.provider": "none",
        "editor.defaultFormatter": null,
        "python.autoComplete.extraPaths": [
            "/home/flo/happyspace/devel/lib/python3/dist-packages",
            "/opt/ros/noetic/lib/python3/dist-packages"
        ],
        "python.analysis.extraPaths": [
            "/home/flo/happyspace/devel/lib/python3/dist-packages",
            "/opt/ros/noetic/lib/python3/dist-packages"
        ]
    }
}

however, upon imports of one package into another, like in

/home/flo/happyspace/src/fmp_tools/src/fmp_tools/execute_poses_record.py

the line

from fmp_tracepen_node.tracepen_node import TracepenNode

gets the error

Import "fmp_tracepen_node.tracepen_node" could not be resolvedPylancereportMissingImports (module) fmp_tracepen_node

while the file to be imported sits in

/home/flo/happyspace/src/fmp_tracepen_node/src/fmp_tracepen_node/tracepen_node.py

what am I doing wong?

edit flag offensive delete link more

Comments

Please do not use the answer section to ask a question. I notice you have already created a new question for yourself. If you think it is relevant, you are encouraged to put a link to this older post In the description section of your new question.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2023-07-22 08:20:20 -0500 )edit

I created this first but then realized makes more sense to create a separate question. Somehow I forgot that it is an "Answers" button, no discussion. Sorry sir!

infraviored gravatar image infraviored  ( 2023-07-24 10:23:48 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2021-07-19 01:51:38 -0500

Seen: 3,645 times

Last updated: Jul 21 '23