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

ROS package that contains python package with a subpackage is not installed properly

asked 2022-08-01 13:50:24 -0500

BlazP gravatar image

I have ROS package that contains a python package with a subpackage (nested python package). When I build the ROS package and source devel workspace, I can import the subpackage as expected. However if I source install workspace I cannot import subpackge anymore, only module of the outer python package (a.py).

Here is example file structure:

my_ws/src/ros_pkg/
    CMakeLists.txt
    package.xml
    setup.py
    src/
        my_pkg/
            __init__.py
            a.py
            my_subpkg/
                __init__.py
                b.py

Content of the setup.py file:

#!/usr/bin/env python
from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup

setup_args = generate_distutils_setup(
    packages=[
        'my_pkg',
    ],
    package_dir={'': 'src'}
)
setup(**setup_args)

Steps to reproduce:

Setup basic ROS package containing python package as provided in example file structure Then build it.

catkin config --install
catkin build my_pkg

Source devel workspace and try to import nested module in python (it works).

source devel/setup.bash
python -c 'from my_pkg import a'  # Works

Now source install workplace (in fresh shell) and try to import it again. It fails.

source devel/setup.bash
python -c 'from my_pkg import a'  # Works
python -c 'from my_pkg.my_subpkg import b'  # Fails (ImportError: No module named my_subpkg)

As suspected, there is no python subpackage in the install workspace (my_ws/install/lib/python2.7/dist-packages /my_pkg/)

my_ws/install/lib/python2.7/dist-packages/my_pkg/
    __init__.py
    a.py
    # my_subpkg/ is not here

I tested it on python 2 and 3 (ROS melodic and noetic).

Can anyone tell me how to achieve that the subpackage would be accessible when the install workspace is sourced?

Full story: I normally source develworkspace when working with ROS, however, I also use Industrial CI (https://github.com/ros-industrial/ind...) where tests are run based on install workspace (if I am not mistaken), which consequentially fails in that case. So if catkin cannot be configured to avoid this issue, maybe Industrial CI can be tweaked to pass tests?

edit retag flag offensive close merge delete

Comments

1 Answer

Sort by » oldest newest most voted
1

answered 2022-08-09 02:21:35 -0500

Mathias Lüdtke gravatar image

This is not a problem with catkin or CI, you just have to fix your setup.py.

From https://docs.python.org/2.7/distutils...:

Keep in mind that although package_dir applies recursively, you must explicitly list all packages in packages: the Distutils will not recursively scan your source tree looking for any directory with an __init__.py file

catkin does not offer any special support here (http://docs.ros.org/en/melodic/api/ca...):

Note: As in setuptools, the packages list is not recursive, and sub-packages must be included explicitly (e.g. your_package.tools.my_util which would contain the python modules defined in the folder src/your_package/tools/my_util/, along with an __init__.py file)

If you want my_pkg.my_subpkg to get installed, you have to list it explicitly.

packages=[
  'my_pkg',
  'my_pkg.my_subpkg', 
],
edit flag offensive delete link more

Comments

Thank you very much. This is exactly what I needed.

BlazP gravatar image BlazP  ( 2022-08-09 10:31:25 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-08-01 13:46:43 -0500

Seen: 626 times

Last updated: Aug 09 '22