Robotics StackExchange | Archived questions

ROS melodic import error "No name hello in module XXXXX" in VScode

Hi there!

I was trying to create a proper structure for my ROS project in python, following this guide: http://wiki.ros.org/rospy_tutorials/Tutorials/Makefile

I successfully got to the end of the tutorial and ran my "hello" script correctly, that printed the message "Hello my friend!"

My question is about VScode. In fact, opening the working "hello" script, located in my_package/bin, gives me this import error in VScode and i can not figure out why...

No name 'hello' in module 'XXXX' pylint(no-name-in-module)
Unable to import 'XXXXX.hello' pylint(import-error)

I have installed the ROS extension as this page said http://wiki.ros.org/IDEs, but nothing changed...

This is not a problem right now, because the script, ran by terminal, works fine, but in a future where my code will be more complex, this VScode issue could be very annoying.

I am not posting my code because it's the same of the link above.

I am new to ROS and i cannot understand why i got this problem, can someone help me?

Thanks a lot

Asked by Ale_Palu on 2020-04-03 05:12:28 UTC

Comments

First stop for all IDE related questions would be wiki/IDEs, specifically, the VSCode section.

Not claiming this will solve all your problems, but please take a look there first.

Asked by gvdhoorn on 2020-04-03 06:10:44 UTC

not quite sure if this solves the problem. however, I've found the VScode plugin does not automatically always pick up the right paths.

What I've found to work quite well is:

  1. create a "ROS workspace" with in VScode with the respective plugin.
  2. source the ros workspace in the built-in terminal
  3. call "ROS: Update Python Path" and "ROS: Update C++ Properties" in VScode, especially after adding new packages.

Asked by mgruhler on 2020-04-03 08:36:36 UTC

Answers

As discussed here, the following is needed:

  1. Place your module in <ws>/src/<pkg-name>/src/<pkg-name>
  2. Create an empty file <ws>/src/<pkg-name>/src/<pkg-name>/__init__.py
  3. Create <ws>/src/<pkg-name>/setup.py with

    from distutils.core import setup from catkin_pkg.python_setup import generate_distutils_setup

    setup_args = generate_distutils_setup() setup_args['packages'] = [''] setup_args['package_dir'] = {'': 'src'}

    setup(**setup_args)

  4. Add catkin_python_setup() to <ws>/src/<pkg-name>/CMakeLists.txt

  5. Build workspace with catkin_make

  6. Open <ws> in VS Code with ROS and Pylint extensions installed

  7. Add src/<pkg-name>/src to "python.autoComplete.extraPaths" in <ws>/.vscode/settings.json

You can then from <pkg-name>.<module-name> import <your-content> from any node script (e.g. <ws>/src/<pkg-name>/scripts/mynode)

Asked by F1iX on 2020-09-30 03:51:50 UTC

Comments