0
$\begingroup$

Rosanswers logo

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...


Originally posted by Sentinal_Bias on ROS Answers with karma: 418 on 2014-04-02

Post score: 2

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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/html/howto/installing_python.html


Originally posted by demmeln with karma: 4306 on 2014-04-02

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by Sentinal_Bias on 2014-04-02:
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

Comment by shoemakerlevy9 on 2016-12-28:
Link is broken. Here is an updated link

$\endgroup$

Your Answer

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