0
$\begingroup$

Rosanswers logo

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.


Originally posted by SilverBullet on ROS Answers with karma: 74 on 2016-03-30

Post score: 1


Original comments

Comment by jbinney on 2016-03-30:
In your setup.py you list 'src' as the package_dir but I don't see any 'src' directory in your package directories.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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

Originally posted by Niranjan with karma: 51 on 2016-03-31

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by SilverBullet on 2016-03-31:
Thanks a lot! It works well for me. :)

$\endgroup$

Your Answer

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