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

rospy ImportError - cannot find custom module under src/ folder

asked 2016-01-07 04:13:41 -0500

K Chen gravatar image

Here are the steps to reproduce:

  1. create a new package under ~/catkin_ws/src:

    catkin_create_pkg foopy rospy
    
  2. create src/foopy folder and add __init__.py in it

  3. create a main.py under scripts/ folder:

#!/usr/bin/env python
# -*- coding: utf-8

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

import sys
for f in sys.path:
    print f
Then chmod +x and execute it using: rosrun foopy main.py It prints:
/home/k/catkin_ws/src/foopy/scripts
/usr/local/lib/python2.7/dist-packages
/home/k/catkin_ws/devel/lib/python2.7/dist-packages
/opt/ros/indigo/lib/python2.7/dist-packages
/usr/lib/python2.7
...

Which does not include module foopy of my src/foopy folder. So when I import anything created under src/foopy/, it will raise an ImportError.

How to solve this issue?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2016-01-07 04:20:24 -0500

gvdhoorn gravatar image

updated 2016-01-07 04:25:55 -0500

How to solve this issue?

Have you created a setup.py and uncommented the catkin_python_setup() line of your CMakeLists.txt?

See wiki/rospy_tutorials/Tutorials/Makefile - Installing scripts and exporting modules and Catkin 0.6.16 documentation » User Guide » Handling of setup.py.


Edit: you also don't mention whether you've rebuilt your workspace before trying to execute your script. Catkin cannot generate the necessary setup.* files (that will setup your PYTHONPATH) if you don't.


Also: import roslib; roslib.load_manifest('foopy') is not necessary anymore in Hydro+.

edit flag offensive delete link more

Comments

Many thanks for your reply! Problem resolved!

K Chen gravatar image K Chen  ( 2016-01-07 11:16:59 -0500 )edit

But it's weird when I am printing out the sys.path, it does not include src path still, but when I import some file under 'src/foopy' using from foopy import foopkg it is working. How did python find foopkg.py?

K Chen gravatar image K Chen  ( 2016-01-07 11:27:13 -0500 )edit

This totally solved my problem, after already having struggled for many hours. I had done everything EXCEPT uncomment catkin_python_setup() in CMakeLists.txt. After doing that, it worked perfectly.

sheaffej gravatar image sheaffej  ( 2018-01-21 13:41:31 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-01-07 04:13:41 -0500

Seen: 2,941 times

Last updated: Jan 07 '16