Ask Your Question
0

Python import: rosrun works well but roslaunch reports error

asked 2016-03-30 20:47:57 -0600

SilverBullet gravatar image

Hi, I'm writing some rospy nodes recently and some import errors ocur when I utilize roslaunch to start my node.

a.py
1 import sys
2 sys.path.append("..")
3 from middle_abstraction.function_unit import FunctionUnit

ImportError: No module named middle_abstraction.function_unit

The file is organized as following

package
    scripts
       basic_support
          a.py
      middle_abstraction
          function_unit.py

When I cd to basic_support and run python a.py, everthing is working well as well as when I utilize rosrun pkg a.py. But when I utilize roslaunch, this error ocurs.

I have written one setup.py file in my pkg folder:

## ! DO NOT MANUALLY INVOKE THIS setup.py, USE CATKIN INSTEAD

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

# fetch values from package.xml
setup_args = generate_distutils_setup(
    packages=['multi_robot_patrol'],#my pkg name
    package_dir={'': 'src'},
    requires=['std_msgs', 'rospy', 'message_filters', 'gps_common', 'sensor_msgs']
)

setup(**setup_args)

I'm really confused that rosrun works pretty well but roslaunch reports import error. I'd appreciate it if anyone can help me.

edit retag flag offensive close merge delete

Comments

In your setup.py you list 'src' as the package_dir but I don't see any 'src' directory in your package directories.

jbinney gravatar imagejbinney ( 2016-03-30 21:24:20 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-03-31 00:28:14 -0600

Niranjan gravatar image

updated 2016-03-31 00:51:37 -0600

you need to change the package_dir in setup.py from "src" to "scripts". And packages in setup.py should be "middle_abstraction", the directory that contains the python file.

## ! DO NOT MANUALLY INVOKE THIS setup.py, USE CATKIN INSTEAD

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

# fetch values from package.xml
setup_args = generate_distutils_setup(
    packages=['middle_abstraction'],#python pkg name
    package_dir={'': 'scripts'},
    requires=['std_msgs', 'rospy', 'message_filters', 'gps_common', 'sensor_msgs']
)

setup(**setup_args)

then create a __init__.py file in the directory middle_abstraction, and don't forget to uncomment the catkin_python_setup() statement in CMakeLists.txt file.

Then after catkin_make, you can import the file like this

a.py

 import sys
 sys.path.append("..")
 import middle_abstraction.function_unit
edit flag offensive delete link more

Comments

Thanks a lot! It works well for me. :)

SilverBullet gravatar imageSilverBullet ( 2016-03-31 19:22:33 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-03-30 20:47:57 -0600

Seen: 276 times

Last updated: Mar 31 '16