ROS Q&A | How to solve the error “ImportError : No module named xxxx.msg”

ROS Q&A | How to solve the error “ImportError : No module named xxxx.msg”

The question that we solved today can be found on the following link:

https://answers.ros.org/question/271620/importerror-no-module-named-xxxxmsg/

ROS Question:

I migrate an installation from one computer to another, switching from Ubuntu 12.04 to 16.04 and from indigo ros to kinetic When I run my installation that has about 30 packages most of the things seem to start well except for a package that fails to make an import I specify that I do not have this error on the computer of the previous configuration, or everything is identical in my programs, except the denomination of indigo and kinetic The error is as follows: From my_folder_msgs.msg import my_file as My_fileMsg
ImportError : No module named my_folder_msgs.msg

Of course, my_folder_msgs.msg exists and appears when I call: rosmsg list.

Would anyone have an idea?


Answer:

When we are developing using ROS, it’s common to define our custom ROS Messages, but sometimes we get stuck about how to use our ROS Messages. A common scenario is that my ROS Messages are identified with rosmsg list but when we try to use the message on our nodes (let’s say a python file), there are errors importing. In this video answer, we solve this error by answering a real question.

When we use our custom messages, even if they are listed with,rosmsg list they only can be used by our nodes after they be compiled.

I have created a video ( https://www.youtube.com/watch?v=NKeebwRNvv8 ) that answers exactly this question.

The steps explained in the video can be done in your own computer but we highly recommend you following the steps using ROSDS (ROS Development Studio), since it’s a free platform and you don’t have to install ROS in your local machine :

Step1. Create a ROSject in ROSDS (ROS Development Studio)

As we said previously, you can easily follow the steps using ROSDS. The advantage is that you can use ROS without having to install ROS. The only thing you need is a Web Browser. In order to create an account, please go to this link.

Once you have an account, you can create a ROSject pressing the button that is shown once you login. Once the ROSject is created, you can open it. At this moment you should have a web Desktop. Now you just need to open a Web Shell and run the commands below.

Step 2. Create a ROS package

We create a package to start to reproduce the problem in it.

$ cd ~/catkin_ws/src
$ catkin_create_pkg my_folder_msgs

The user had a problem with the custom messages, we can create it with the following steps.

$ cd my_folder_msgs
$ mkdir msg
$ touch msg/my_file.msg

Then we put a test message into the my_file.msg file with the following code.

float32 rosds

As a test, we create a python file called question.py under the my_folder_msgs/src directory with the following commands:

mkdir -p ~/catkin_ws/src/my_folder_msgs/src
touch ~/catkin_ws/src/my_folder_msgs/src/question.py

After having created the question.py file, we add the following content to it

#! /usr/bin/env python
from my_folder_msgs.msg import my_file as My_fileMsg
print '\n\nYes, it worked!!!'

If we execute the file now with the python question.py command we got the same error as the question.

ImportError: No module named my_folder_msgs.msg

Step 3. Compile messages

It turns out you have to compile your message before you can use it in the package.

  1. You have to compile your message. For that, you have to touch the package.xml and CMakeLists.txt located on ~/catkin_ws/src/my_folder_msgs.  The original file generate by ROS actually contains the lines you need but comment out. You just have to remove the comment to use it. In package.xml remove the comments before the line <build_depend>message_generation</build_depend> and  <exec_depend>message_runtime</exec_depend> . In the CMakeLists.txt, you have to add message_generation and std_msgs into the find_package(), so it becomes find_package(catkin REQUIRED message_generation std_msgs). After that you have to uncomment  the add_message_files() part and add my_file.msg in it and finally uncomment the generate_messages() part. After preparing the package.xml and CMakeLists.txt, you run catkin_make on your catkin_ws with cd ~/catkin_ws; catkin_make
  2. After the message is compiled, you have to source the setup.bash like: ource ~/catkin_ws/devel/setup.bash
  3. Now you can import your messages without any problem:
cd ~/catkin_ws/src/my_folder_msgs/src;
python question.py

Take away today:

In order to use your customized message, remember to compile and source it before you import it into another file.

Please, remember to subscribe to our channel on YouTube and press the bell if you to be notified of new videos we publish. Additionally, feel free to leave your thoughts on the commends section of the video (https://www.youtube.com/watch?v=NKeebwRNvv8).

ROS courses related:

Pin It on Pinterest