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

Importing python module from another ROS package setup.py

asked 2014-04-02 01:26:52 -0500

Sentinal_Bias gravatar image

Hi

I am trying to import a python module from another ROS package.

My package is utils 
utils
-----/src
------------file_io.py (python module) 
------------__init__.py

I have a node in Package A that wants to import a function in file_io.py

So far I have created a setup.py file for the package utils

    ## ! 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=['utils'],
    package_dir={'': 'src'},
    requires=['roscpp', 'rospy', 'tf']
)

setup(**setup_args)

In my cmakeList file I made the following changes

install(PROGRAMS src/file_io.py
    DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})

and uncommented #catkin_python_setup()

In the package.xml of package A, I made the utils package a build and run dependency, modified the cmakList of package A and added the package utils to the find_package(catkin required COMPONENTS).

from utils.file_io import extractor results in the import error: No module name file_io.

Am I missing something? To be honest I did not really understand the tutorial, I am kind of new to python.

As I understand in order to import a python module it has to be in your python path, I thought this setup thing was meant to make ROS automatically add the files to the python path of any package that depends on utils...

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2014-04-02 02:33:13 -0500

demmeln gravatar image

I have no experience with this myself, but from the catkin documentation, it seems that if you want to only install the module utils, your should remove the

install(PROGRAMS src/file_io.py
    DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})

from your CMakeLists.txt and move the two python files in src into another subfolder utils. Not sure if you need the requires in the setup.py.

I suggest to read the following page, in particular the "Modules" section carefully: http://docs.ros.org/hydro/api/catkin/...

edit flag offensive delete link more

Comments

1

thanks the modules import works now. In the Rospy tutorial it installs a python executable using the setup.py even though the catkin documentation mentions not to at the end... And also you do not need the 'requires' in setup.py

Sentinal_Bias gravatar image Sentinal_Bias  ( 2014-04-02 03:27:45 -0500 )edit

Link is broken. Here is an updated link

shoemakerlevy9 gravatar image shoemakerlevy9  ( 2016-12-28 12:22:27 -0500 )edit
0

answered 2017-07-10 05:42:33 -0500

RDaneelOlivaw gravatar image

updated 2017-07-11 09:50:10 -0500

I've uploaded this video Tutorial to show you an example of how to import to import python modules from other packages, or from any python realted scripts. Also how to install them. Hope it helps ;). Python import Module Tutorial


UPDATE: following @gvdhoorn observation, here I leave the essential code to follow the video. Essentialy to import a python module from one package inside any other or even python stand alone programs you have to do the following: 1) Create a folder inside the src folder of your package, named as your package. Lets say your package is called "common_tools_pkg". Well you should have somthing like this: common_tools_pkg:

--->src:

--->--->common_tools_pkg:

--->--->-->__init__.py

--->--->--->mycommonmodule.py

2) Then Uncoment in the CMakelists.txt the line: catkin_python_setup()

3) Create inside the package a python file called "setup.py" with the contents:

from distutils.core import setup

from catkin_pkg.python_setup import generate_distutils_setup

setup_args = generate_distutils_setup(

packages=['common_tools_pkg'],

package_dir={'': 'src'},

)

setup(**setup_args)

4) Then catkinmake and source deve/setup.bash, and you will now be able to import that puython module anywhere.

edit flag offensive delete link more

Comments

1

@RDaneelOlivaw: could you please include a textual description of what the video shows in your answer? If the video disappears (not uncommon), your answer would become useless.

gvdhoorn gravatar image gvdhoorn  ( 2017-07-10 05:49:44 -0500 )edit
1
gvdhoorn gravatar image gvdhoorn  ( 2017-07-11 10:04:33 -0500 )edit
0

answered 2014-04-02 01:57:20 -0500

hawesie gravatar image
edit flag offensive delete link more

Comments

1

This only works with the rosbuild workspace unfortunately. I am using the catkin build system for groovy :(

Sentinal_Bias gravatar image Sentinal_Bias  ( 2014-04-02 01:59:00 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2014-04-02 01:26:52 -0500

Seen: 3,390 times

Last updated: Jul 11 '17