0
$\begingroup$

Rosanswers logo

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

edit:

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

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

2 directories, 6 files

The content of my __init__.py looks like this

from coordinate import Coordinate

The content of my setup.py

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

setup(**setup_args)

The content of my CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)
project(myproject_common_lib)

find_package(catkin REQUIRED COMPONENTS
  rospy
)

catkin_python_setup()

catkin_package()

include_directories(
   $(catkin_INCLUDE_DIRS)
)

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)

devel
├── lib
│   └── python2.7
│       └── dist-packages
│           └── myproject_common_lib
│               ├── __init__.py
│               └── __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


Originally posted by Westranger on ROS Answers with karma: 165 on 2017-11-20

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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

Originally posted by rreignier with karma: 544 on 2017-11-20

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by Westranger on 2017-11-20:
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 '__init__.py', which comes from PyCharm

Comment by jayess on 2017-11-20:
Did you add an __init__.py file to the directory that contains your libraries/modules?

Comment by Westranger on 2017-11-21:
@jayess and @rreignier I updated my post with more information. Actually I cannot see any difference between my stuff and the provided tutorial

Comment by Westranger on 2017-11-21:
I found the error in the CMakeLists.txt the $(catkin_INCLUDE_DIRS) part must be written with {}

$\endgroup$

Your Answer

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