# Mixed python/cpp Ament package

**URL:** <https://discourse.openrobotics.org/t/mixed-python-cpp-ament-package/1452>\
**Category:** ROS General\
**Tags:** ros2\
**Created:** [March 8, 2017, 5:05pm UTC](https://discourse.openrobotics.org/t/mixed-python-cpp-ament-package/1452 "2017-03-08T17:05:02Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![kensley](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/kensley/32/546_2.png) [@kensley](https://discourse.openrobotics.org/u/kensley)\
**Post date:** [March 8, 2017, 5:05pm UTC](https://discourse.openrobotics.org/t/mixed-python-cpp-ament-package/1452/1 "2017-03-08T17:05:02Z")

</div>

Hello all,

I have yet to see a mixed python/cpp ament package. Does anyone have an example or documentation to point to?

Thanks.

---

<div class="post-metadata">

**Author:** ![dirk-thomas](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/dirk-thomas/32/4328_2.png) [@dirk-thomas](https://discourse.openrobotics.org/u/dirk-thomas)\
**Post date:** [March 8, 2017, 5:47pm UTC](https://discourse.openrobotics.org/t/mixed-python-cpp-ament-package/1452/2 "2017-03-08T17:47:47Z")

</div>

`catkin` provided a CMake function to introspect a `setup.py` and for a subset of the possible content do “the right thing”.

In ament the package `ament_cmake_python` provides two functions:

- [ament\_python\_install\_package](https://github.com/ament/ament_cmake/blob/7b31d859f3d42b9eb6c84db807d95a5604a6881b/ament_cmake_python/cmake/ament_python_install_package.cmake#L16-L22)
- [ament\_python\_install\_module](https://github.com/ament/ament_cmake/blob/7b31d859f3d42b9eb6c84db807d95a5604a6881b/ament_cmake_python/cmake/ament_python_install_module.cmake#L16-L22)

Those should cover the major need to install Python packages or individual modules. At the moment there is no support for additional features provided by setuptools like entry points.

---

<div class="post-metadata">

**Author:** ![srsidd](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/srsidd/32/551_2.png) [@srsidd](https://discourse.openrobotics.org/u/srsidd)\
**Post date:** [March 8, 2017, 10:17pm UTC](https://discourse.openrobotics.org/t/mixed-python-cpp-ament-package/1452/3 "2017-03-08T22:17:26Z")

</div>

How would a `cpp` file be integrated into a pakage within the `setup.py`? Would it be defined as an external module?

---

<div class="post-metadata">

**Author:** ![dirk-thomas](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/dirk-thomas/32/4328_2.png) [@dirk-thomas](https://discourse.openrobotics.org/u/dirk-thomas)\
**Post date:** [March 8, 2017, 10:50pm UTC](https://discourse.openrobotics.org/t/mixed-python-cpp-ament-package/1452/4 "2017-03-08T22:50:02Z")

</div>

Python packages with a `setup.py` file are processed in a standard way. So you can do whatever you like in there which works for `python setup.py install`, e.g. [C++ extensions](https://docs.python.org/3/distutils/setupscript.html#extension-source-files).

---

<div class="post-metadata">

**Author:** ![srsidd](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/srsidd/32/551_2.png) [@srsidd](https://discourse.openrobotics.org/u/srsidd)\
**Post date:** [March 9, 2017, 10:03pm UTC](https://discourse.openrobotics.org/t/mixed-python-cpp-ament-package/1452/5 "2017-03-09T22:03:14Z")

</div>

That works if my cpp file is not a ros2 node and a generic cpp file. But if it were to be a ros2 node, how would I define that in my setup.py? Is there a way to do this?

---

<div class="post-metadata">

**Author:** ![dirk-thomas](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/dirk-thomas/32/4328_2.png) [@dirk-thomas](https://discourse.openrobotics.org/u/dirk-thomas)\
**Post date:** [March 9, 2017, 10:17pm UTC](https://discourse.openrobotics.org/t/mixed-python-cpp-ament-package/1452/6 "2017-03-09T22:17:52Z")

</div>

We have never tried that. CMake and `ament_cmake` provide a lot of features to handle dependencies and build libraries and executables from C++ code. I would assume it is a lot of effort trying to mimic this within a `setup.py` file.

Maybe the other way around makes more sense: using more Python specific features by calling out to Python from CMake.

---

<div class="post-metadata">

**Author:** ![srsidd](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/srsidd/32/551_2.png) [@srsidd](https://discourse.openrobotics.org/u/srsidd)\
**Post date:** [March 10, 2017, 1:31am UTC](https://discourse.openrobotics.org/t/mixed-python-cpp-ament-package/1452/7 "2017-03-10T01:31:30Z")

</div>

Does that mean including ament\_python in the CMakeLists.txt file and compiling my python node?

If I were to do this, how would I go about it? It’d be really helpful if you could give me a general sense of direction..

---

<div class="post-metadata">

**Author:** ![marguedas](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/marguedas/32/1633_2.png) [@marguedas](https://discourse.openrobotics.org/u/marguedas)\
**Post date:** [March 10, 2017, 1:52am UTC](https://discourse.openrobotics.org/t/mixed-python-cpp-ament-package/1452/8 "2017-03-10T01:52:50Z")

</div>

We don’t have many examples of this but I think rclpy[1] can give you an idea. It is an `ament_cmake` package[2] that compiles C code into a library and install it[3]. I also installs python modules[4] for the user to interact with. The difference here is that the libraries are installed in the python install directories (because they are python extensions) but you can install them where C++ libraries should go.

Hope this helps

[1][GitHub - ros2/rclpy: rclpy (ROS Client Library for Python)](https://github.com/ros2/rclpy)  
[2][rclpy/rclpy/package.xml at a8253b7b6538a25162f5abfba45dfebe2e7d3186 · ros2/rclpy · GitHub](https://github.com/ros2/rclpy/blob/a8253b7b6538a25162f5abfba45dfebe2e7d3186/rclpy/package.xml#L27)  
[3][rclpy/rclpy/CMakeLists.txt at a8253b7b6538a25162f5abfba45dfebe2e7d3186 · ros2/rclpy · GitHub](https://github.com/ros2/rclpy/blob/a8253b7b6538a25162f5abfba45dfebe2e7d3186/rclpy/CMakeLists.txt#L79)  
[4][rclpy/rclpy/CMakeLists.txt at a8253b7b6538a25162f5abfba45dfebe2e7d3186 · ros2/rclpy · GitHub](https://github.com/ros2/rclpy/blob/a8253b7b6538a25162f5abfba45dfebe2e7d3186/rclpy/CMakeLists.txt#L27)

---

<div class="post-metadata">

**Author:** ![srsidd](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/srsidd/32/551_2.png) [@srsidd](https://discourse.openrobotics.org/u/srsidd)\
**Post date:** [March 13, 2017, 6:09pm UTC](https://discourse.openrobotics.org/t/mixed-python-cpp-ament-package/1452/9 "2017-03-13T18:09:16Z")

</div>

Okay, this is what I have right now. My package has the following structure -

```
- my_pkg
    - CMakeLists.txt
    - package.xml
    - src
        - talker.cpp
    - my_pkg
        - __init__.py
        - listener.py
    - setup.py

```

My CMakeLists.txt looks like this -

```
cmake_minimum_required(VERSION 3.5)

project(my_pkg)

if(NOT WIN32)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra -Wpedantic")
endif()

find_package(ament_cmake REQUIRED)
find_package(ament_cmake_python REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(python_cmake_module REQUIRED)
find_package(PythonExtra MODULE)

ament_python_install_package(${PROJECT_NAME})

add_executable(${PROJECT_NAME}_talker src/talker.cpp)
ament_target_dependencies(${PROJECT_NAME}_talker rclcpp std_msgs)

install(TARGETS
  ${PROJECT_NAME}_talker
  DESTINATION bin
)

ament_package()

```

My `setup.py` looks like this -  
from setuptools import setup, Extension

```
setup(
    name='my_pkg',
    version='0.0.0',
    packages=[],
    py_modules=[
        'my_pkg.listener',],
    install_requires=['setuptools'],
    author='user',
    author_email='user@gmail.com',
    maintainer='user
    maintainer_email='user@gmail.com',
    keywords=['ROS'],
    classifiers=[
        'Intended Audience :: Developers',
        'License :: OSI Approved :: Apache Software License',
        'Programming Language :: Python',
        'Topic :: Software Development',
    ],
    description='Mixed Ament package for python and cpp files.',
    license='Apache License, Version 2.0',
    test_suite='test',
    entry_points={
        'console_scripts': [
            'my_pkg_listener = my_pkg.listener:main',
        ],
    },
)

```

I am able to compile the package, and generate an executable for `my_pkg_talker`.  
However `ament` fails to recognize the `setup.py` and install a target for the python node. Is there something else I need to do specifically for ament to recognize the existence of a `setup.py` and run it?

---

<div class="post-metadata">

**Author:** ![wjwwood](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/wjwwood/32/13715_2.png) [@wjwwood](https://discourse.openrobotics.org/u/wjwwood)\
**Post date:** [March 15, 2017, 5:44am UTC](https://discourse.openrobotics.org/t/mixed-python-cpp-ament-package/1452/10 "2017-03-15T05:44:10Z")

</div>

It looks like you’re mixing CMake (you have a `CMakeLists.txt`) and Python’s setuptools (you have a full `setup.py` file as well). Our build tool doesn’t support dual build system packages like this. When building your package it will either run `cmake` or `python setup.py ...` but not both.

If you need both then another solution will need to be found. Perhaps calling the `setup.py` from the `CMakeLists.txt` or replacing the functionality of the `setup.py` with just cmake code or replacing the functionality of the CMake code within `setup.py` using [C++ Extensions](https://docs.python.org/3/distutils/setupscript.html#extension-source-files), which @dirk-thomas linked to before.

We don’t currently have such an example as you’ve setup here. Specifically something that contains non-trivial CMake code (which would not easily be replicated with Python’s C++ Extensions) as well as a `setup.py` file which uses `entry_points` . Unfortunately, all I can say is that we don’t have a solution for this yet and I don’t know what the best strategy forward is at the moment.

---

<div class="post-metadata">

**Author:** ![srsidd](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/srsidd/32/551_2.png) [@srsidd](https://discourse.openrobotics.org/u/srsidd)\
**Post date:** [March 15, 2017, 8:46pm UTC](https://discourse.openrobotics.org/t/mixed-python-cpp-ament-package/1452/11 "2017-03-15T20:46:19Z")

</div>

Ah okay. Thanks. I will try experimenting a little on my own and see if I can come up with something. If I’m successful I can create a new post and share my experience.

As suggested by @dirk-thomas, using `CMake` to call the `setup.py` would be the easier option. Is there any resources that you’d be aware of that could help me to proceed in this direction?

---

<div class="post-metadata">

**Author:** ![wjwwood](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/wjwwood/32/13715_2.png) [@wjwwood](https://discourse.openrobotics.org/u/wjwwood)\
**Post date:** [March 24, 2017, 10:44pm UTC](https://discourse.openrobotics.org/t/mixed-python-cpp-ament-package/1452/12 "2017-03-24T22:44:54Z")

</div>

The closest thing I know of is how catkin from ROS 1 combines CMake and Python:

- [catkin/cmake/interrogate\_setup\_dot\_py.py at kinetic-devel · ros/catkin · GitHub](https://github.com/ros/catkin/blob/kinetic-devel/cmake/interrogate_setup_dot_py.py)
- [catkin/cmake/catkin\_python\_setup.cmake at 5a7f61d594a56ca4b3ae1705e64140e9f29e0bd7 · ros/catkin · GitHub](https://github.com/ros/catkin/blob/5a7f61d594a56ca4b3ae1705e64140e9f29e0bd7/cmake/catkin_python_setup.cmake#L63)

---

<div class="post-metadata">

**Author:** ![Jacob\_Beck](https://avatars.discourse-cdn.com/v4/letter/j/a9a28c/32.png) [@Jacob\_Beck](https://discourse.openrobotics.org/u/Jacob_Beck)\
**Post date:** [November 1, 2018, 12:24am UTC](https://discourse.openrobotics.org/t/mixed-python-cpp-ament-package/1452/13 "2018-11-01T00:24:32Z")

</div>

@srsidd, did you make any progress with this? I’m struggling with the same issue right now.

---

<div class="post-metadata">

**Author:** ![ruffsl](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/ruffsl/32/83_2.png) [@ruffsl](https://discourse.openrobotics.org/u/ruffsl)\
**Post date:** [February 10, 2019, 5:29am UTC](https://discourse.openrobotics.org/t/mixed-python-cpp-ament-package/1452/14 "2019-02-10T05:29:13Z")

</div>

Just chiming in to say I also have the same package layload as @srsidd exemplified above, and would also be interested in how to get around this.

> [@wjwwood](#):
>
> We don’t currently have such an example as you’ve setup here. Specifically something that contains non-trivial CMake code (which would not easily be replicated with Python’s C++ Extensions) as well as a `setup.py` file which uses `entry_points` . Unfortunately, all I can say is that we don’t have a solution for this yet and I don’t know what the best strategy forward is at the moment.

Wrt. ros2 packages using both C++ and Python with CMake and `entry_points`, is this still the case?

---

<div class="post-metadata">

**Author:** ![dirk-thomas](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/dirk-thomas/32/4328_2.png) [@dirk-thomas](https://discourse.openrobotics.org/u/dirk-thomas)\
**Post date:** [February 11, 2019, 5:18pm UTC](https://discourse.openrobotics.org/t/mixed-python-cpp-ament-package/1452/15 "2019-02-11T17:18:50Z")

</div>

> [@ruffsl](#):
>
> Wrt. ros2 packages using both C++ and Python with CMake and `entry_points` , is this still the case?

Nobody has worked on that and afaik nobody is planning to do so. So the state is still the same.
