2

I have a ROS2-python (rclpy) package with multiple subpackages that work together. I would like to cythonize the package in order to hide the source code and just leave the executables to do the job. Any idea how could I do that with the setup.py of the package and the launch files ?

1 Answer 1

3

So I would answer my own question in case someone faced the same issue. For cythonizing your ros2 package in python the best way is adding the required cython components to the setup.py of each package separately. I have marked the cython parts (with <--) for a better understranding.

Please note that after building the package using colcon, a copy of the python source codes would be stored under the install directory produced by colcon. You can easily delete them manually and the package should run with the generated c and shared object files.

from glob import glob
from setuptools import setup
from Cython.Build import cythonize <--
import os

package_name = 'your_package_name_here'

files = package_name + "/*.py"

setup(
    ext_modules=cythonize(files,compiler_directives={'language_level' : "3"},force=True,quiet=True), <--
    name=package_name,
    version='0.0.0',
    packages=[package_name],
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
        (os.path.join('share', package_name), glob('launch/*.launch.py'))
    ],
    install_requires=['setuptools', "wheel",  "Cython"], <--
    zip_safe=True,
    maintainer='NAME',
    maintainer_email='EMAIL',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'SOME CODE HERE',
        ],
    },
)
Sign up to request clarification or add additional context in comments.

3 Comments

I have similar case to yours, after compilation I found the problem with logging in ROS2 node caused changing logger severity, have you ever encountered similar errors?
I cannot remember seeing an error like this. Can you maybe give some more details or post the whole error?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.