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

Colcon Build error using Cython in setup.py [closed]

asked 2021-03-09 10:24:34 -0500

anonymous user

Anonymous

updated 2021-03-13 10:52:06 -0500

Hi,
I'm using ROS2 Eloquent on Ubuntu 18.04 and im trying to use from Cython.Build import cythonize in my setup.py.
My package contains the file cythontest.pyx and the first time i launch colcon build i get an error but the correspronding cythontest.c is created in the same directory. When launching colcon build again and the error is gone and i can launch my node with the method from cythontest.pyx. But if the cythontest.c gets deleted the same pattern occurs again.
To clarify I tested this file outside of the ros2 envoriment and it works fine.

My setup.py

from setuptools import setup, find_packages
from Cython.Build import cythonize

package_name = 'object_detector'

setup(
    ext_modules=cythonize("object_detector/cythontest.pyx"),
    name=package_name,
    version='0.0.0',
    packages=find_packages(exclude=['test']),
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
    ],
    install_requires=['setuptools', "wheel", "Cython"],
    zip_safe=True,
    maintainer='simon',
    maintainer_email='...',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'object_detection = object_detector.object_detection:main'
        ],
    },
)

The following error occurs

colcon build
Starting >>> detection_data
Starting >>> realsense2_camera_msgs
Starting >>> camera_calibration
Starting >>> realsense2_description
Finished <<< realsense2_description [0.95s]                              
Finished <<< camera_calibration [1.35s]                                   
Finished <<< realsense2_camera_msgs [1.59s]                               
Starting >>> realsense2_camera
Finished <<< detection_data [1.65s]
Starting >>> object_detector
/home/simon/.local/lib/python3.6/site-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /home/simon/sciebo/Uni/7_Semester/Bachelor_Arbeit/IS_Bachelor_Arbeit/Bachelor_Thesis/Datentraeger_Dateien/Software/IS/dev_ws/src/object_detector/object_detector/cythontest.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)
--- stderr: object_detector                                                
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/colcon_core/executor/__init__.py", line 91, in __call__
    rc = await self.task(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/colcon_core/task/__init__.py", line 93, in __call__
    return await task_method(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/colcon_ros/task/ament_python/build.py", line 51, in build
    setup_py_data = get_setup_data(self.context.pkg, env)
  File "/usr/lib/python3/dist-packages/colcon_core/task/python/__init__.py", line 20, in get_setup_data
    return dict(pkg.metadata[key](env))
  File "/usr/lib/python3/dist-packages/colcon_ros/package_identification/ros.py", line 130, in getter
    desc.path / 'setup.py', env=env)
  File "/usr/lib/python3/dist-packages/colcon_python_setup_py/package_identification/python_setup_py.py", line 242, in get_setup_information
    setup_py, env=env)
  File "/usr/lib/python3/dist-packages/colcon_python_setup_py/package_identification/python_setup_py.py", line 286, in _get_setup_information
    return ast.literal_eval(output)
  File "/usr/lib/python3.6/ast.py", line 48, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "/usr/lib/python3.6/ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 1
    Compiling object_detector/cythontest.pyx because it changed.
                            ^
SyntaxError: invalid syntax
---
Failed   <<< object_detector [0.87s, exited with code 1]
Aborted  <<< realsense2_camera [0.91s]

Summary: 4 packages finished [2.80s]
  1 package failed: object_detector
  1 package aborted: realsense2_camera
  1 package had stderr output: object_detector
invalid syntax (<unknown>, line 1)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-03-10 08:19:49 -0500

anonymous user

Anonymous

updated 2021-03-20 13:06:04 -0500

Solved it.

Cython prints error, warning, or status messages during the compilation and this is somehow interfering with the colcon build process. So the solution would be to set Cython quiet so it wont print error, warning, or status messages during the compilation like this: cythonize(...,quiet=True)

My new setup.py

from setuptools import setup, find_packages, Extension
from Cython.Build import cythonize
import glob
package_name = 'object_detector'



sourcefiles = glob.glob("example/*.pyx")

extensions = [Extension("example.cythontest", sourcefiles,define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")])]


setup(
    ext_modules=cythonize(extensions,compiler_directives={'language_level' : "3"},force=True,annotate=True,quiet=True),
    name=package_name,
    version='0.0.0',
    packages=find_packages(exclude=['test']),
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
    ],
    install_requires=['setuptools', "wheel", "Cython"],
    zip_safe=False,
    maintainer='simon',
    maintainer_email='',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'object_detection = object_detector.object_detection:main'
        ],
    },
)

The problem then is that only minimal amount of feedback is given.

edit flag offensive delete link more

Comments

hey! I just wanted to ask if you know, after I cythonize my packages, does using ros2 run pkg_name node_name use the original python files or the new cythonized files? I'm trying to improve runtimes by cythonizing my project. thanks in advance

JRAxelrad gravatar image JRAxelrad  ( 2023-04-17 08:25:23 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2021-03-09 10:24:34 -0500

Seen: 780 times

Last updated: Mar 20 '21