pythonros

ROS2 Node runs using python3 command but not with ros2 run


I am using a YOLOv5 package I found online (https://drive.google.com/drive/folders/1xC-gFmWQybQGagxtu8WxGqpFS-1Ek-V2) for my project. The thing is, the tutorial says to run the detection code by going to its location and running python3 ros_recognition_yolo.py, but I want to make the node executable from ros2 run. I made the node executable, but when I run it, it doesn't work. The strange is that running like before works, only not with ros2 run. When I run, it gives me this error:

Traceback (most recent call last):
  File "/home/gabriel_pogo/at_work/install/yolobot_recognition/lib/yolobot_recognition/ros_recognition_yolo.py", line 20, in <module>
    from models.experimental import attempt_load
ModuleNotFoundError: No module named 'models'

Models is a folder with lots of important scripts. Like the code I want to run, it is inside the scripts folder in the yolobot_recognition pkg. If it helps, here is my CMake and my package.xml:

cmake_minimum_required(VERSION 3.5)
project(yolobot_recognition)

# Default to C99
if(NOT CMAKE_C_STANDARD)
  set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(ament_cmake_python REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclpy REQUIRED)

set(dependencies
  "rclpy"
  "rclcpp"
)

install(
  DIRECTORY scripts/models scripts/utils
  DESTINATION share/${PROJECT_NAME}
)

install(PROGRAMS
  scripts/ros_recognition_yolo.py
  DESTINATION lib/${PROJECT_NAME}
)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # uncomment the line when a copyright and license is not present in all source files
  #set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # uncomment the line when this package is not in a git repo
  #set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_export_dependencies(${dependencies})
ament_package()
<?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>yolobot_recognition</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="robotmania@todo.todo">robotmania</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>
  <buildtool_depend>ament_cmake_python</buildtool_depend>


  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>
  <exec_depend>rclpy</exec_depend>
  <exec_depend>models</exec_depend>
  <build_depend>models</build_depend>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

Solution

  • It's because you're installing the modules in a different location than your script. As such your PYTHONPATH and import paths won't line up. You should be able to make this change:

    install(
        DIRECTORY scripts/models scripts/utils
        DESTINATION lib/${PROJECT_NAME}
    )
    

    I also generally recommend that pure python ROS packages are created as a Python ROS package