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

How-to-solve-the-error-ImportError-No-module-named-xxxx.msg

Written by Ruben Alves

06/10/2017

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:

Topics: ImportError
Masterclass 2023 batch2 blog banner

Check Out These Related Posts

129. ros2ai

129. ros2ai

I would like to dedicate this episode to all the ROS Developers who believe that ChatGPT or...

read more

16 Comments

  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!!

    Reply
  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*”`

    Reply
  3. Tong

    hi. I have the problem too. I cannot fix it by your method.
    ROS_ROOT=/opt/ros/kinetic/share/ros
    ROS_PACKAGE_PATH=/media/z/LinuxData/data/project/read_pcd_python/src:/opt/ros/kinetic/share
    ROS_MASTER_URI=http://localhost:11311
    ROS_VERSION=1
    ROSLISP_PACKAGE_DIRECTORIES=/media/z/LinuxData/data/project/read_pcd_python/devel/share/common-lisp
    ROS_DISTRO=kinetic
    ROS_ETC_DIR=/opt/ros/kinetic/etc/ros

    ./devel/lib/python2.7/dist-packages/usb_com/msg/_gnss.py
    ./devel/share/gennodejs/ros/usb_com/msg/gnss.js
    ./devel/share/roseus/ros/usb_com/msg/gnss.l
    ./devel/share/common-lisp/ros/usb_com/msg/_package_gnss.lisp
    ./devel/share/common-lisp/ros/usb_com/msg/gnss.lisp
    ./devel/include/usb_com/gnss.h
    ./src/usb_com/msg/gnss.msg
    ./src/usb_com/msg/gnss.msg.zip
    ./build/usb_com/CMakeFiles/_usb_com_generate_messages_check_deps_gnss.dir

    Reply
  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?

    Reply
  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)
    project(usb_com)
    find_package(catkin REQUIRED)
    find_package(catkin REQUIRED COMPONENTS
    message_generation
    std_msgs
    rospy
    )
    add_message_files(FILES
    gnss.msg
    )
    generate_messages(DEPENDENCIES
    std_msgs
    )
    catkin_package(
    CATKIN_DEPENDS
    message_runtime
    )
    include_directories(
    ${catkin_INCLUDE_DIRS}
    )
    install(PROGRAMS
    src/usb_com.py
    DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
    )

    Here is the part of Package.xml:
    catkin
    message_generation
    roscpp
    rospy
    message_generation
    roscpp
    rospy
    message_runtime
    roscpp
    rospy

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

    Reply
  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?

    https://www.youtube.com/watch?v=NKeebwRNvv8

    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.

    Reply
    • 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:

      usb_com
      0.0.0
      The usb_com package
      <!– Jane Doe –>
      TODO
      The usb_com package
      catkin

      message_generation
      roscpp
      rospy
      message_generation
      roscpp
      rospy
      message_runtime
      roscpp
      rospy

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

      Reply
      • Tong

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

        Reply
    • 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/usb_com_old.py”, line 7, in
      from usb_com.msg import gnss
      File “/media/zhenwei-qian/LinuxData/data/project/read_pcd_python/src/usb_com/src/usb_com.py”, line 10, in
      from usb_com.msg import gnss
      ImportError: No module named msg

      Reply
  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?

    Reply
    • Anonymous

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

      Reply
  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 (https://rds.theconstructsim.com/r/rosject_new/), 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.

    Reply
    • Tong

      Hi,Alves,
      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.

      Reply
      • Ruben Alves

        Another user recently experienced the same problem.

        As we can see, in the solution (https://get-help.robotigniteacademy.com/t/exercise-4-3-ros-basics-python/15271/22), the problem was because somehow the compiled message file was prefixed by an “_” (underline)

        The user message was Age.msg, and the generated python message was named _Age.py instead of Age.py (I have no idea why).

        Just renaming the _Age.py to Age.py solved the problem.

        The command used to rename it was (in this case, the user pkg name was my_examples_pkg):

        cp -v ~/catkin_ws/devel/lib/python3/dist-packages/my_examples_pkg/msg/_Age.py ~/catkin_ws/devel/lib/python3/dist-packages/my_examples_pkg/msg/Age.py

        Reply
  9. AH Arnob

    hi there im using ros noetic on ubuntu 20.04 with python 3.6. im trying openai_ros_examples project. it require to have pysdf under catkin workspace. but when i try to use it i get the following error. please help!!!
    Traceback (most recent call last):
    File “”, line 1, in
    File “/home/humanoid/catkin_ws/devel/lib/python3/dist-packages/pysdf/__init__.py”, line 34, in
    exec(__fh.read())
    File “”, line 1, in
    ModuleNotFoundError: No module named ‘parse’

    Reply
    • Ruben Alves

      Hi @AH Arnob.

      It seems you have to install a package named “parse”.

      This may be also caused when you have two different python versions in the same computer (like python3 and python2)

      In theory, you should solve it just with: “pip install parse” or “pip3 install parse”.

      Reply

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