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

ImportError with custom action in python

asked 2013-06-12 04:44:52 -0600

updated 2014-01-28 17:16:52 -0600

ngrennan gravatar image

Ubuntu 12.04, Groovy, catkin build

I am trying to define a custom action for a rospy node. starts with:

#!/usr/bin/env python

import actionlib
import rospy
from geometry_msgs.msg import Twist
from my_foo.msg import *

When I run the node with rosrun, it yields the error:

Traceback (most recent call last):
  File "/home/johndoe/groovy-workspace/src/dcslROS/my_package/src/my_package/", line 16, in <module>
    import my_foo.msg
  File "/home/johndoe/groovy-workspace/src/dcslROS/my_package/src/my_package/", line 16, in <module>
    import my_foo.msg
ImportError: No module named msg

I have tried renaming the python package to not match the ROS package name and changing the file accordingly but that yielded the same error. The message build files are generated into devel/lib/python2.7/dist-packages/my_foo/msg but are not being found at run time.

My file structure is:


#!/usr/bin/env python

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

d = generate_distutils_setup(
    package_dir={'': 'src'}

setup(**d) is an empty file.

CustomAction.action contains:

# Determine whether to connect
bool connect # True = connect robot, False = Do nothing
# Determine if successfully connected
bool connected # True = Connected, False = Not connected
# Determine if robot is still connecting
bool in_progress # True = still connecting, False = finished


  <maintainer email="">
    John Doe

  <url type="website"></url>
  <!-- <url type="bugtracker"></url> -->

  <author>John Doe</author>


  <!-- <build_depend>joy</build_depend> -->
  <!-- <build_depend>turtlebot_teleop</build_depend> -->
  <!-- <test_depend>std_msgs</test_depend> -->
  <!-- <test_depend>geometry_msgs</test_depend> -->
  <!-- <test_depend>rospy</test_depend> -->
  <!-- <test_depend>roscpp</test_depend> -->
  <!-- <test_depend>joy</test_depend> -->
  <!-- <test_depend>turtlebot_teleop</test_depend> -->




cmake_minimum_required(VERSION 2.8.3)

# Load catkin and all dependencies required for this package
set(CATKIN_DEPS std_msgs geometry_msgs rospy roscpp genmsg actionlib actionlib_msgs)
set(SYS_DEPS python-bluez python-sys)

# Set the build type.  Options are:
#  Coverage       : w/ debug symbols, w/o optimization, w/ code-coverage
#  Debug          : w/ debug symbols, w/o optimization
#  Release        : w/o debug symbols, w/ optimization
#  RelWithDebInfo : w/ debug symbols, w/ optimization
#  MinSizeRel     : w/o debug symbols, w/ optimization, stripped binaries
#set(ROS_BUILD_TYPE RelWithDebInfo)

# Add action files
  DIRECTORY action
  FILES CustomAction.action

# Install python scripts using distutils

# Generate action messages
  DEPENDENCIES actionlib_msgs

## DEPENDS: system dependencies of this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## LIBRARIES: libraries you create in this project that dependent projects also ...
edit retag flag offensive close merge delete


Have you sourced the setup.bash file from the devel folder after build the workspace?

Dirk Thomas gravatar image Dirk Thomas  ( 2013-06-12 08:13:19 -0600 )edit

Yes, I have.

Brendan Andrade gravatar image Brendan Andrade  ( 2013-06-13 04:58:33 -0600 )edit

Just a guess but the node you invoke is Is there a Python file in the same folder with the same name as the package?

Dirk Thomas gravatar image Dirk Thomas  ( 2013-06-13 07:57:43 -0600 )edit

No there isn't. When I get a free moment, I'm going to investigate what the python path is when the node runs. Files are generating correctly in devel but then aren't found when the node runs. Could be b/c the python package within the package folder structure has the same name as the ros package?

Brendan Andrade gravatar image Brendan Andrade  ( 2013-06-13 09:28:16 -0600 )edit

It is very common that the Python package has the same name as the ROS package - actually recommended.

Dirk Thomas gravatar image Dirk Thomas  ( 2013-06-13 09:48:58 -0600 )edit

A good next step would be to put all the code in a repo (if not already) and post the link to the repo with the exact command sequence you execute.

Dirk Thomas gravatar image Dirk Thomas  ( 2013-06-13 09:50:31 -0600 )edit

My mistake, yes the the node file had the same name as the package. I'm sorry my question did not reflect that. Changing the name of the node solved the problem. Was python node names not matching package names a requirement added with groovy/catkin or was that required with rosbuild/fuerte too?

Brendan Andrade gravatar image Brendan Andrade  ( 2013-06-14 06:19:16 -0600 )edit

Thank you, Dirk, for your help! Would you prefer to answer to the question below or should I save you the time and write the answer myself?

Brendan Andrade gravatar image Brendan Andrade  ( 2013-06-14 06:20:34 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2013-06-14 07:21:05 -0600

Dirk Thomas gravatar image

updated 2013-06-14 07:22:21 -0600

This issue is not even related to ROS but to how Python resolves imports. The problem is that you can not import a package "my_foo" from with a Python file "". In your example the script imports 'itself' and does not find "msg" inside of it.

If the script itself is not on the sys.path you can work around the issue by using "from __future__ import absolute_import" ( But in the case your script is part of sys.path (which is the case if you e.g. invoke it from within the same folder) you have to make both name different (usually by renaming the script).

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2013-06-12 04:44:52 -0600

Seen: 3,253 times

Last updated: Jun 14 '13