0
$\begingroup$

Rosanswers logo

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.


Originally posted by dgerod on ROS Answers with karma: 113 on 2013-02-12

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by dgerod with karma: 113 on 2013-02-24

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.