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
.
├── AppRunner.py
├── ApplicationRunner.ui
├── GUIpublisher.py
├── GUIsubscriber.py
├── GUIsubscriberThreads.py
├── __init__.py
├── __pycache__
│   ├── AppRunner.cpython-310.pyc
│   ├── GUIsubscriberThreads.cpython-310.pyc
│   ├── ThreadClasses.cpython-310.pyc
│   ├── __init__.cpython-310.pyc
│   └── my_first_node.cpython-310.pyc
├── countdown_client.py
├── countdown_server.py
├── my_first_node.py
├── publisher_example.ui
├── submodules
│   ├── AppRunnerThreads.py
│   ├── __init__.py
│   └── __pycache__
│       └── AppRunnerThreads.cpython-310.pyc
└── subscriber_example.ui

Specifically, the AppRunner.py 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 AppRunner.py directly looks like this:

henry@DESKTOP-9QLI4DN:~/ros2_ws/src/my_robot_controller/my_robot_controller$ python3 AppRunner.py
Traceback (most recent call last):
  File "/home/henry/ros2_ws/src/my_robot_controller/my_robot_controller/AppRunner.py", 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/__init__.py", line 957, in distribution
    return Distribution.from_name(distribution_name)
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", 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 setup.py 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'

setup(
    name=package_name,
    version='0.0.0',
    packages=[package_name, submodules],
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='None',
    maintainer_email='None@None.com',
    description='ROS2 Node for launching an application',
    license='Private',
    tests_require=['pytest'],
    entry_points={
        '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="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>my_robot_controller</name>
  <version>0.0.0</version>
  <description>Learning ROS2</description>
  <maintainer email="henry@todo.todo">henry</maintainer>
  <license>There is no license</license>

  <depend>rclpy</depend>
  <depend>ThreadClasses< ...
(more)
edit retag flag offensive close merge delete

Comments

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