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

ImportError with custom action in python

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

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

ngrennan gravatar image

Ubuntu 12.04, Groovy, catkin build

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

my_foo.py 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/my_foo.py", line 16, in <module>
    import my_foo.msg
  File "/home/johndoe/groovy-workspace/src/dcslROS/my_package/src/my_package/my_foo.py", 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 setup.py 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:

/my_foo
  package.xml
  CMakeLists.txt
  setup.py
  /action
    CustomAction.action
  /src
    my_node1.cpp
    /my_foo
      my_foo.py
      __init__.py

Setup.py:

#!/usr/bin/env python

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

d = generate_distutils_setup(
    packages=['my_foo'],
    package_dir={'': 'src'}
)

setup(**d)

__init__.py 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

package.xml

<package>
  <name>my_foo</name>
  <version>0.1.0</version>
  <description>
    Description
  </description>
  <maintainer email="foo@aol.com">
    John Doe
  </maintainer>
  <license>BSD</license>

  <url type="website">http://ros.org/wiki/my_foo</url>
  <!-- <url type="bugtracker"></url> -->

  <author>John Doe</author>

  <buildtool_depend>catkin</buildtool_depend>

  <build_depend>std_msgs</build_depend>
  <build_depend>geometry_msgs</build_depend>
  <build_depend>rospy</build_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>actionlib</build_depend>
  <build_depend>actionlib_msgs</build_depend>
  <build_depend>genmsg</build_depend>
  <!-- <build_depend>joy</build_depend> -->
  <!-- <build_depend>turtlebot_teleop</build_depend> -->
  <run_depend>std_msgs</run_depend>
  <run_depend>geometry_msgs</run_depend>
  <run_depend>rospy</run_depend>
  <run_depend>roscpp</run_depend>
  <run_depend>actionlib</run_depend>
  <run_depend>actionlib_msgs</run_depend>
  <run_depend>genmsg</run_depend>
  <run_depend>joy</run_depend>
  <run_depend>turtlebot_teleop</run_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> -->

  <export>

  </export>
</package>

CMakeLists.txt

# http://ros.org/doc/groovy/api/catkin/html/user_guide/supposed.html
cmake_minimum_required(VERSION 2.8.3)
project(my_foo)

# 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)
find_package(catkin REQUIRED COMPONENTS ${CATKIN_DEPS})

# 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
add_action_files(
  DIRECTORY action
  FILES CustomAction.action
)

# Install python scripts using distutils
catkin_python_setup()


# Generate action messages
generate_messages(
  DEPENDENCIES actionlib_msgs
)

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

Comments

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 -0500 )edit

Yes, I have.

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

Just a guess but the node you invoke is my_node2.py. 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 -0500 )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 -0500 )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 -0500 )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 -0500 )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 -0500 )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 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

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

Dirk Thomas gravatar image

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

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 "my_foo.py". 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" (http://www.python.org/dev/peps/pep-0328/). 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

Stats

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

Seen: 3,418 times

Last updated: Jun 14 '13