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


Written by Ruben Alves


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

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?


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 ( ) 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 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/

After having created the 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 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;

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 (

ROS courses related:

Topics: ImportError

Check Out These Related Posts


  1. Amir

    I have done literally all that but no way to compile it and still not able to import it, more prcisely i import all the messages in Ubuntu Indigo without any problem but never worked in Kinectic! WIRED!!

  2. Ruben Alves

    Hi Amir,

    could you tell us what is the output of the commands below:
    source ~/catkin_ws/devel/setup.bash
    env | grep ROS

    Also, what is the name of your message? If `MyMsg.msg`, could you tell us which is the output of the command below?

    `find ~/catkin_ws -name “*MyMsg*”`

  3. Tong

    hi. I have the problem too. I cannot fix it by your method.


  4. Ruben Alves

    Hi Tong,

    have you compiled your message correctly? What is the message name?

    Could you paste here the code used to import the message?

  5. Tong

    Hi, Alves, thanks a lot for your answer firstly.
    I have compiled my message and I think it compiled correctly, because i use “rosmsg show usb_com/gnss” or “rosmag list” can display my message correctly. It shown as followings:
    LinuxData/data/project/read_pcd_python$ rosmsg show usb_com/gnss
    std_msgs/Header header
    uint32 seq
    time stamp
    string frame_id
    float64 GPSWeek
    float64 GPSTime
    float64 heading
    float64 pitch
    float64 roll
    float64 lattitude
    float64 longtitude
    float64 altitude
    float64 ve
    float64 vn
    float64 vu
    float64 baseline
    float64 NSV1
    float64 NSV2

    Here is the CMakeList.txt :
    cmake_minimum_required(VERSION 2.8.3)
    find_package(catkin REQUIRED)
    find_package(catkin REQUIRED COMPONENTS

    Here is the part of Package.xml:

    I cannot find errors in above files.
    My computer OS is Ubuntu16.04, and ROS is kinetic.
    Can you help me to fix it?

  6. Ruben Alves

    Hi Tong,

    even if `rosmsg show` can find your message, it doesn’t mean it was compiled.
    This is explained in the video. Have you checked it?

    You have to compile with `cd ~/catkin_ws; catkin_make`
    In the end, you have to have a message saying `100% compiled`.

    If it doesn’t compile, can you paste here the content of the CMakeLists.txt? Please remember to check the video.

    • Anonymous

      hi, I have checked the vedio. And I think i had done the tips mentioned in the video. But I still count the problem when running the python scripts. The CMakeList.txt and package.xml has pasted in last reply. Please check it. But there should update the package.xml because of missing something.
      Here is the whole package.xml:

      The usb_com package
      <!– Jane Doe –>
      The usb_com package


      Somethings should be noticed is that I can import the stand messages of the ROS software and it’s plugins.

      • Tong

        I can not paste the “” tags of package.xml because of the Wed. Please Know that.

    • Tong

      Here is my code:
      #!/usr/bin/env python

      import rospy
      import serial
      import time
      import threading
      from usb_com.msg import gnss
      print “hello”

      Here is the bugs:
      Traceback (most recent call last):
      File “/media/zhenwei-qian/LinuxData/data/project/read_pcd_python/src/usb_com/src/”, line 7, in
      from usb_com.msg import gnss
      File “/media/zhenwei-qian/LinuxData/data/project/read_pcd_python/src/usb_com/src/”, line 10, in
      from usb_com.msg import gnss
      ImportError: No module named msg

  7. Tong

    I find somethings very strange!
    If your project name of the package don’t contain “_”,it can work normally even though there is a message “cann’t import xxx.msg module” in RoboWare IDE. And if you project name of the package containing “_”,it cann’t work.
    Does anyone find this problem and can fix it?

    • Anonymous

      is there a file named in your code?if there is, rename it another name

  8. Ruben Alves

    Hi Tong,

    I suppose the error is when trying to import `from usb_com.msg import gnss`, right?

    Remember that you have to compile your message. I couldn’t find the instructions to compile them on your CMakeLists.txt

    In order to make your code work, I’d need to have a look at all the code (if you don’t mind making it public).
    For that, I’d need you to create a ROSJect on ROSDS (, put your code there, save the ROSject and share the Public Link with me.

    Otherwise is difficult to solve the problem with just parts of the code.

    • Tong

      Sorry. I think my cmakelist.txt is right because of I am using ROS for a long time. Please check it again. The reason I have post before. I suppose it may be a bug for ROS Kinetic under Ubuntu16.04.


Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This