Gonam Salinsky
Gonam Salinsky

Reputation: 1

ROS2 ModuleNotFoundError “No module named ‘configs’”. Cannot import modules into node

I am working on a ROS2 project and encountering an issue where my node cannot import modules from the configs package. Below is a detailed description of the problem, including the structure of my project, the error message, and steps I've already taken to troubleshoot.

test_ws/
├── src/
│   └── test_package/
│       ├── configs/
│       │   ├── __init__.py
│       │   └── global_settings.py
│       ├── test_package/
│       │   ├── __init__.py
│       │   ├── gps_path_planning_node.py
│       │   └── scripts/
│       │       ├── __init__.py
│       │       └── utils.py
│       ├── package.xml
│       ├── setup.cfg
│       └── setup.py

When I run the node using ros2 launch test_package simulation_launch.py, I encounter the following error:

[gps_path_planning_node-5] Traceback (most recent call last):
[gps_path_planning_node-5]   File "/home/hiber/Desktop/test_ws/install/test_package/lib/test_package/gps_path_planning_node", line 33, in <module>
[gps_path_planning_node-5]     sys.exit(load_entry_point('test-package==0.0.0', 'console_scripts', 'gps_path_planning_node')())
...
[gps_path_planning_node-5] ModuleNotFoundError: No module named 'configs'

Code Snippet (gps_path_planning_node.py):

from rclpy.node import Node
import rclpy
from configs import global_settings
from test_package.scripts.utils import *

class GPSPathPlanningNode(Node):
    def __init__(self):
        super().__init__('gps_path_planning_node')
        self.max_speed = EGO_VEHICLE_MAX_SPEED
        self._logger.info('Successfully launched!')

def main(args=None):
    rclpy.init(args=args)
    node = GPSPathPlanningNode()
    rclpy.spin(node)
    node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

The contents of setup.py are as follows:

import os
from setuptools import setup

PACKAGE_NAME = 'test_package'

def generate_data_files(share_path, data_files_path):
    data_files_temp = []

    for path, _, files in os.walk(data_files_path):
        data_file = (
            os.path.dirname(os.path.dirname(share_path)) + '/' + path, [
                os.path.join(path, file) for file in files if not file.startswith('.')
            ]
        )

        data_files_temp.append(data_file)

    return data_files_temp

data_files = []

data_files.append(('share/ament_index/resource_index/packages', ['resource/' + PACKAGE_NAME]))
data_files.append(('share/' + PACKAGE_NAME, ['package.xml']))

data_files += generate_data_files('share/' + PACKAGE_NAME + '/configs/', 'configs/')
data_files += generate_data_files('share/' + PACKAGE_NAME + '/launch/', 'launch/')
data_files += generate_data_files('share/' + PACKAGE_NAME + '/resource/', 'resource/')

setup(
    name=PACKAGE_NAME, 
    version='0.0.0', 
    packages=[PACKAGE_NAME], 
    data_files=data_files, 
    install_requires=['setuptools'], 
    zip_safe=True, 
    maintainer='hiber', 
    maintainer_email='...', 
    description='TODO: Package description', 
    license='TODO: License declaration', 
    tests_require=['pytest'], 
    entry_points={
        'console_scripts': [
            # 'ackermann_keyboard_teleop_node = test_package.ackermann_keyboard_teleop_node:main', 
            'gps_path_planning_node = test_package.gps_path_planning_node:main', 
            'projection_weight_matrices_node = test_package.projection_weight_matrices_node:main', 
            'surround_view_node = test_package.surround_view_node:main', 
        ], 
    }, 
)

Environment Details:

  1. ROS2 Distribution: Humble
  2. Python Version: 3.10
  3. OS: Ubuntu 22.04

Request for Help: I would appreciate any guidance on resolving this issue. Specifically:

  1. Why might the module still not be found despite being present in the installed package?
  2. Are there additional configuration steps I may have missed?

Thank you!

Steps Taken So Far:

  1. Moved global_settings.py: I moved global_settings.py to the same directory as gps_path_planning_node.py and updated the import statement to from test_package.global_settings import *. However, other modules from other folders are not found, such as utils.py from scripts/
  2. Checked setup.py: Verified that setup.py includes all necessary packages (test_package and sub-packages like scripts).
  3. Rebuilt the Project: Ran colcon clean followed by colcon build --symlink-install and sourced the workspace.
  4. Tested Import Locally: Running python3 -c "from test_package.global_settings import *; print('Import successful')" works without errors.
  5. I read somewhere that the path install/test_package/lib/python3.10/site-packages/test_package/ should contain the other modules (the same configs/ with global_settings.py and scripts/ with utils.py). Is this really the case?
  6. The __init__.py file is in all directories.
  7. Tried all kinds of imports (relative, absolute, dynamic)

Despite the first four steps, the error continues to occur when running the node via ROS2.

Upvotes: 0

Views: 17

Answers (0)

Related Questions