pythonpackagenodesros2

ROS2 separate directory for Python nodes


Is it possible to have a separate directory for Python nodes in ROS2 (foxy)?

python_pkg/
│
├─ python_pkg/
│  ├─ __init__.py
│  ├─ my_module.py
│  └─ hello_world.py
|
├─ nodes/
│  ├─ __init__.py
│  └─ simple_node.py
|
└─ ...

In setup.py I have tried adding:

setup(
    name="python_pkg",
    ...
    entry_points={
        'console_scripts': [
                'hello_world = python_pkg.hello_world:main',
                'simple_node = nodes.simple_node:main',
        ],
},
)

If I source the workspace and run ros2 run python_pkg simple_node I get the error ModuleNotFoundError: No module named 'nodes'.


Solution

  • I ran into this problem today and came across your question. The way to do it is by adding your node in your data_files first, so in setup.py:

    data_files=[
        ...,
        ('lib/' + package_name, ['./nodes' + '/simple_node.py'])
    ],
    

    Then add the node to your entry_points:

    entry_points={
        'console_scripts': [
            "simple_node = simple_node:main",
        ],
    },