ROS2 Python not importing my module

asked 2023-06-27 18:09:27 -0500

HenryInUtah gravatar image

Hello, I've seen similar questions here and I tried to follow those directions but I just don't understand what I am doing wrong. I am writing a Python ROS2 node and want to separate some code into a module for organization. The package I made for learning and following tutorials is a little cluttered but it looks like this:

henry@DESKTOP-9QLI4DN:~/ros2_ws/src/my_robot_controller/my_robot_controller$ tree
├── ApplicationRunner.ui
├── __pycache__
│   ├── AppRunner.cpython-310.pyc
│   ├── GUIsubscriberThreads.cpython-310.pyc
│   ├── ThreadClasses.cpython-310.pyc
│   ├── __init__.cpython-310.pyc
│   └── my_first_node.cpython-310.pyc
├── publisher_example.ui
├── submodules
│   ├──
│   ├──
│   └── __pycache__
│       └── AppRunnerThreads.cpython-310.pyc
└── subscriber_example.ui

Specifically, the is trying to import AppRunnerThreads from the submodules folder like this:

import sys
import os
#import AppRunnerThreads

from .submodules.AppRunnerThreads import MonitorApplication, RunApplication

The error message when I run directly looks like this:

henry@DESKTOP-9QLI4DN:~/ros2_ws/src/my_robot_controller/my_robot_controller$ python3
Traceback (most recent call last):
  File "/home/henry/ros2_ws/src/my_robot_controller/my_robot_controller/", line 5, in <module>
    from .submodules.AppRunnerThreads import MonitorApplication, RunApplication
ImportError: attempted relative import with no known parent package

When I try to run it from ros2 using the entry point, the error message is a bit more complicated.

henry@DESKTOP-9QLI4DN:~/ros2_ws/src/my_robot_controller/my_robot_controller$ ros2 run my_robot_controller app_runner
Traceback (most recent call last):
  File "/home/henry/ros2_ws/install/my_robot_controller/lib/my_robot_controller/app_runner", line 33, in <module>
    sys.exit(load_entry_point('my-robot-controller', 'console_scripts', 'app_runner')())
  File "/home/henry/ros2_ws/install/my_robot_controller/lib/my_robot_controller/app_runner", line 22, in importlib_load_entry_point
    for entry_point in distribution(dist_name).entry_points
  File "/usr/lib/python3.10/importlib/metadata/", line 957, in distribution
    return Distribution.from_name(distribution_name)
  File "/usr/lib/python3.10/importlib/metadata/", line 548, in from_name
    raise PackageNotFoundError(name)
importlib.metadata.PackageNotFoundError: No package metadata was found for my-robot-controller
[ros2run]: Process exited with failure 1

It appears that I am not configuring something so that the module is available. This whole build process is foreign to me, so I am messing something up.

I don't have enough points to upload files (?) so I will include what I think is important below.

Here is the from the /home/henry/ros2_ws/scr/my_robot_controller folder

from setuptools import setup

package_name = 'my_robot_controller'
submodules = 'my_robot_controller/submodules'

    packages=[package_name, submodules],
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
    description='ROS2 Node for launching an application',
        'console_scripts': [
            "test_node = my_robot_controller.my_first_node:main",
            "app_runner= my_robot_controller.AppRunner:main"

Here is the package.xml from the same folder

<?xml version="1.0"?>
<?xml-model href="" schematypens=""?>
<package format="3">
  <description>Learning ROS2</description>
  <maintainer email="henry@todo.todo">henry</maintainer>
  <license>There is no license</license>

  <depend>ThreadClasses< ...
edit retag flag offensive close merge delete


Can you try to define the submodule as <package_name>.<submodule> and see if that works?

sampreets3 gravatar image sampreets3  ( 2023-06-29 10:58:42 -0500 )edit