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

How to import Python modules to my node?

asked 2018-02-19 10:02:27 -0500

FerCipri gravatar image

I am using ROS Kinetic with Ubuntu 16.04 LTS.

I am trying to connect two ROS nodes (publisher/subscriber, both of them). In one Python node, I am trying to use some modules I had already programmed, which are embedded in pyib2c module.

import pyib2c

This module also imports some Python modules. When I try to run my node, I receive the following error:

ImportError: cannot import name XXXXX , where XXXXX are the modules imported by my pyib2c module.

I have already uncommented the * catkin_python_setup() * in the CMakeList of my package. The setup.py is:

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=['pyib2c'],
    package_dir={'': 'src'})

setup(**setup_args)

I have also created a __init.py__ file which is:

 from . import tabulate
 from . import pyib2c

My Python node, the module pyib2c, the __init.py__ and the setup.py are them all at the same level in my_package/src. However, it turns out to display the same mistake if I keep the pyib2c in a src folder.

How can I make my node import the modules I need?

edit retag flag offensive close merge delete

Comments

Have you compiled your package? In order for you to use the module in your package catkin needs to run your setup.py.

jayess gravatar image jayess  ( 2018-02-21 01:30:23 -0500 )edit

Yes, I have run catkin_make at my catkin_ws and it works perfectly. The trouble comes at the point of rosrun my node.

FerCipri gravatar image FerCipri  ( 2018-02-21 11:28:23 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2018-02-21 01:13:28 -0500

mgruhler gravatar image

The module (i.e. the source files) as well as the __init__.pyneeds to be placed in <path_to_your_package>/src/<your_package> (at least by convention and as explained in the documentation, it might work another way).

The setup.py needs to be in <path_to_your_package>.

Then catkin_python_setup should be able to find everything correctly and, hopefully, you should be able to import everything properly.

edit flag offensive delete link more

Comments

I have tried as you said and didn't work. In fact, it stopped recognising my pyib2c python module =S

FerCipri gravatar image FerCipri  ( 2018-02-21 11:38:33 -0500 )edit
1

Did you run catkin_make once more after reorganizing? You can check if it has been installed properly if the module can be found below devel/lib/python2.7/<yourpackage>, if i am correct.

mgruhler gravatar image mgruhler  ( 2018-02-22 01:54:00 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2018-02-19 10:02:27 -0500

Seen: 10,812 times

Last updated: Feb 21 '18