Ask Your Question
1

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

Info:

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 setup.py bdist_wheel, and it does indeed generate the required .so files, which work correctly from within python.

For ease, here is the setup.py file that does that:

from setuptools import setup
from setuptools_rust import Binding, RustExtension

package_name = 'rust_test'

setup(
    name=package_name,
    version='0.1.0',
    packages=[package_name],
    rust_extensions=[RustExtension("rust_test.rust_test", binding=Binding.PyO3)],
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
    ],
    install_requires=['setuptools','wheel','setuptools_rust'],
    include_package_data=True,
    zip_safe=False,
    maintainer='schoen',
    maintainer_email='schoen@todo.todo',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        '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/__init__.py", line 91, in __call__
    rc = await self.task(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/colcon_core/task/__init__.py", line 93, in __call__
    return await task_method(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/colcon_ros/task/ament_python/build.py", 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/__init__.py", line 20, in get_setup_data
    return dict(pkg.metadata[key](env))
  File "/usr/local/lib/python3.9/site-packages/colcon_ros/package_identification/ros.py", line 129, in getter
    return get_setup_information(
  File "/usr/local/lib/python3.9/site-packages/colcon_python_setup_py/package_identification/python_setup_py.py", 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/python_setup_py.py", 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/ast.py", 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/ast.py", 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 ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

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

nuclearsandwich gravatar image

colcon's support for building python packages using setup.py likely doesn't cover the setuptools_rust extensions you're adding. This is how colcon executes build tasks for setup.py https://github.com/colcon/colcon-core...

In fact I'm not aware of any ROS packages with native compilation requirements using setup.py / 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.https://github.com/colcon/colcon...

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

Stats

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

Seen: 133 times

Last updated: Dec 18 '20