Ask Your Question

colcon python package build error with rust extension

asked 2020-12-15 15:03:30 -0500

AndrewJSchoen gravatar image

updated 2020-12-15 15:11:58 -0500


ROS 2 Version: Foxy

OS: macOS Big Sur

Python: 3.9

colcon_python_setup_py: 0.2.7

I have been looking for ways to get some rust-based code to play nicely with a bunch of python-based ROS 2 code. Given that the last I checked, official ROS2 bindings were not quite working for rust, I thought I might be able to wrap up the rust code (which is really standalone) I had into a single module, and then create bindings for it with PyO3. That way, after building, I could simply import the rust library within python and use ROS 2 at that point.

I created a super-simple test package. I have gotten it to work when cd-ing into the package and running python3 bdist_wheel, and it does indeed generate the required .so files, which work correctly from within python.

For ease, here is the file that does that:

from setuptools import setup
from setuptools_rust import Binding, RustExtension

package_name = 'rust_test'

    rust_extensions=[RustExtension("rust_test.rust_test", binding=Binding.PyO3)],
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
    description='TODO: Package description',
    license='TODO: License declaration',
        'console_scripts': [

However, when I execute colcon build --symlink-install --packages-select rust_test, it fails with the following error:

Starting >>> rust_test
--- stderr: rust_test                   
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/colcon_core/executor/", line 91, in __call__
    rc = await self.task(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/colcon_core/task/", line 93, in __call__
    return await task_method(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/colcon_ros/task/ament_python/", line 51, in build
    setup_py_data = get_setup_data(self.context.pkg, env)
  File "/usr/local/lib/python3.9/site-packages/colcon_core/task/python/", line 20, in get_setup_data
    return dict(pkg.metadata[key](env))
  File "/usr/local/lib/python3.9/site-packages/colcon_ros/package_identification/", line 129, in getter
    return get_setup_information(
  File "/usr/local/lib/python3.9/site-packages/colcon_python_setup_py/package_identification/", line 241, in get_setup_information
    _setup_information_cache[hashable_env] = _get_setup_information(
  File "/usr/local/lib/python3.9/site-packages/colcon_python_setup_py/package_identification/", line 286, in _get_setup_information
    return ast.literal_eval(output)
  File "/usr/local/Cellar/python@3.9/3.9.0_5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/", line 62, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "/usr/local/Cellar/python@3.9/3.9.0_5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    {'package_data': {}, 'dist_files': [], 'src_root': None, 'dependency_links': [], 'setup_requires': [], 'convert_2to3_doctests': [], 'eager_resources': None, 'entry_points': {'console_scripts': []}, 'exclude_package_data': None, 'extras_require': {}, 'include_package_data': True, 'install_requires': ['setuptools', 'wheel', 'setuptools_rust'], 'namespace_packages': None, 'packages': ['rust_test'], 'python_requires': None, 'test_loader': None, 'test_runner': None, 'test_suite': None, 'tests_require': ['pytest'], 'use_2to3': None, 'use_2to3_exclude_fixers': None, 'use_2to3_fixers': None, 'zip_safe': False, 'rust_extensions': [<setuptools_rust.extension.RustExtension object at 0x104bb6e20>], 'cffi_modules': None, 'verbose': 1, 'dry_run ...
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-12-18 13:25:20 -0500

nuclearsandwich gravatar image

colcon's support for building python packages using likely doesn't cover the setuptools_rust extensions you're adding. This is how colcon executes build tasks for

In fact I'm not aware of any ROS packages with native compilation requirements using / setuptools. rclpy is a CMake project which includes python code rather than a Python project that includes native extensions for example.

You might be able to take the same approach and use colcon-cargo to build your Rust + Python project but I am not sure.

Lastly, it might be possible to use CMake as the glue logic to include both your Rust and Python code.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2020-12-15 15:03:30 -0500

Seen: 133 times

Last updated: Dec 18 '20