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

How to share common lib's in ROS workspace

asked 2017-11-20 04:22:03 -0600

Westranger gravatar image

updated 2017-11-21 02:37:08 -0600

I'm having several packages in my workspace and I'm currently at the point where a want to avoid code duplicity. At the moment I have package called myproject_common in which all msg and srv files are located. After building the workspace they are properly available in all my other packages. Now I want to my common .py files into the common packages and also make them available. After calling catkin_make the msg and srv classes are available under ./devel/lib/python2.7/dist-packages/ which is quiet nice because this makes them also available in my IDE without the need to import something manually. How can I accomplish this with self written common python files? What do I need to change in my CMakeLists.txt to get this working


I separated the msg / srv from the common modules, because the msg / srv stuff work fine so now my pkg with the shared modules is named myproject_common_lib

this is my directory structure

├── CMakeLists.txt
├── package.xml
└── src
    └── myproject_common_lib
        ├── coordinate.pyc

2 directories, 6 files

The content of my looks like this

from coordinate import Coordinate

The content of my


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

# fetch values from package.xml
setup_args = generate_distutils_setup(
    package_dir={'': 'src'},


The content of my CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)

find_package(catkin REQUIRED COMPONENTS




When I want to import the shared module I use the following import (which does not work)

from myproject_common_lib.coordinate import Coordinate

The content of my devel folder looks like this (short version)

├── lib
│   └── python2.7
│       └── dist-packages
│           └── myproject_common_lib
│               ├──
│               └── __init__.pyc
└── share
    ├── myproject_common_lib
    │   └── cmake
    │       ├── myproject_common_libConfig.cmake
    │       └── myproject_common_libConfig-version.cmake

I cannot see any difference to the posted tutorial. Also when watching this video I cannot see any difference :-(

edit 2:

This is what PyCharm sees

image description

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2017-11-20 06:44:14 -0600

rreignier gravatar image

You have to write Python modules (= libraries) and declare them, see this tutorial.

So your modules will be available from your other packages and you will be able to import them using:

import myproject_common.my_module
edit flag offensive delete link more


When I follow the tutorial it works for the script but it does not work for my modules. The import cannot be found. The error I get is Cannot find reference 'coordinate' in '', which comes from PyCharm

Westranger gravatar image Westranger  ( 2017-11-20 10:26:22 -0600 )edit

Did you add an file to the directory that contains your libraries/modules?

jayess gravatar image jayess  ( 2017-11-20 11:54:22 -0600 )edit

@jayess and @rreignier I updated my post with more information. Actually I cannot see any difference between my stuff and the provided tutorial

Westranger gravatar image Westranger  ( 2017-11-21 02:22:37 -0600 )edit

I found the error in the CMakeLists.txt the $(catkin_INCLUDE_DIRS) part must be written with {}

Westranger gravatar image Westranger  ( 2017-11-21 09:03:51 -0600 )edit

Question Tools



Asked: 2017-11-20 04:22:03 -0600

Seen: 617 times

Last updated: Nov 21 '17