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

ImportError when launching a ROS node done in Python

asked 2013-02-12 04:04:56 -0500

dgerod gravatar image

updated 2014-01-28 17:15:12 -0500

ngrennan gravatar image

When I launch a ROS node made in Python, the "ImportError: No module named scripts.scene" is raised

I have created the node using Python and following < http://www.ros.org/wiki/PyStyleGuide >. My layout is similar to "small modules with no msg/srvs".

anchor_mod
 |- src/
    |- anchor_mod.py
 |- scripts/
    |- scene/
       |- __init.py__
       |- object.py
       |- simple_motion.py
    |- utils/
       |- __init.py__
       |- trajectories.py
    |- conceptual_spaces/
    |- scene/
    |- __init.py__
    |- pmm.py
    |- srm.py
 |- tests/
    |- test_gen_trajectory.py

And my "anchor_mod.py" is importing some classes (i.e Object) and functions from "scripts" folder.

#!/usr/bin/env python
import roslib; roslib.load_manifest('anchor_mod')
import rospy

from scripts.scene import object

if __name__ == '__main__':
    obj = object.Object

When I launch the script by hand:

anchor_mod/src> ./anchor_mod.py

or using "rosrun":

rosrun anchor_mod anchor_mod.py

The following error is risen:

  File "/home/dieesrod/Workspaces/ROS/fuerte/sandbox/chsys_exp/anchor_mod/src/anchor_mod.py", line 12, in <module>
    from scripts.scene import object
ImportError: No module named scripts.scene

This error occurs whatever module from "script" folder is imported.

Why this error is risen? Do I have to add my "non-exporting python files" to ROSPATH, PYTHONPATH or other place? How I have to import modules? Do I have to use "import scripts.scene" or only "import scene"?

Thanks.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2013-02-24 04:57:12 -0500

dgerod gravatar image

updated 2013-02-26 21:15:21 -0500

I am still don't know the reason for that problem but I fixed it.

Now I am able to launch a python node with internal modules in "/scripts" folder as explained in ROS python guide.

For fixing it I have to do two things:

  • Add a dependency to himself in "manifest.xml" of the package
  <depend package="std_msgs"/>
  <depend package="rospy"/>
  <depend package="roscpp"/>
  <depend package="tf"/>
  <depend package="smartcam_msgs"/>
  <depend package="anchor_mod"/>
  • And add "/script" directory to python path in execution.

For solving the second point I have created a module named "rospy_helper" that adds the "/scripts" directory to python path when the node starts. I use it after importing "rospy".

 import roslib; roslib.load_manifest('anchor_mod')
 import rospy

 import rospy_helper; rospy_helper.add_modules_to_path();

Although I have solved the problem, if someone knows the reason please explain me. I would like to understand what is really happening. Thanks.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-02-12 04:04:56 -0500

Seen: 1,749 times

Last updated: Feb 26 '13