Class 2: "name 'launch_ros' is not defined"

Hi!
I am having issues when launching the exercise 2.1 on ROS2.

The launch file is called my_package_launch_file.launch.py and it contains:

from launch import LaunchDescription

from launch_ros.actions import Node, LoadComposableNodes

def generate_launch_description():

return LaunchDescription([

    launch_ros.actions.Node(

        package='my_package',

        executable='simple',

        output='screen')

])

Then I launch it using the following command line:
user:~/ros2_ws$ ros2 launch my_package my_package_launch_file.launch.py

I’ve already build the project using colcon build, and then source install/setup.bash

[INFO] [launch]: All log files can be found below /home/user/.ros/log/2021-10-16-22-52-21-926965-1_xterm-4197
[INFO] [launch]: Default logging verbosity is set to INFO
Task exception was never retrieved
future: <Task finished name=‘Task-2’ coro=<LaunchService._process_one_event() done, defined at /opt/ros/foxy/lib/python3.8/site-packages/launch/launch_service.py:271> exception=InvalidLaunchFileError(‘py’)>
Traceback (most recent call last):
File “/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_description_sources/any_launch_file_utilities.py”, line 53, in get_launch_description_from_any_launch_file
return loader(launch_file_path)
File “/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_description_sources/python_launch_file_utilities.py”, line 68, in get_launch_description_from_python_launch_file
return getattr(launch_file_module, ‘generate_launch_description’)()
File “/home/user/ros2_ws/install/my_package/share/my_package/my_package_launch_file.launch.py”, line 6, in generate_launch_description
launch_ros.actions.Node(
NameError: name ‘launch_ros’ is not defined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_service.py”, line 273, in _process_one_event
await self.__process_event(next_event)
File “/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_service.py”, line 293, in __process_event
visit_all_entities_and_collect_futures(entity, self.__context))
File “/opt/ros/foxy/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py”, line 45, in visit_all_entities_and_collect_futures
futures_to_return += visit_all_entities_and_collect_futures(sub_entity, context)
File “/opt/ros/foxy/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py”, line 45, in visit_all_entities_and_collect_futures
futures_to_return += visit_all_entities_and_collect_futures(sub_entity, context)
File “/opt/ros/foxy/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py”, line 38, in visit_all_entities_and_collect_futures
sub_entities = entity.visit(context)
File “/opt/ros/foxy/lib/python3.8/site-packages/launch/action.py”, line 108, in visit
return self.execute(context)
File “/opt/ros/foxy/lib/python3.8/site-packages/launch/actions/include_launch_description.py”, line 125, in execute
launch_description = self.__launch_description_source.get_launch_description(context)
File “/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_description_source.py”, line 84, inget_launch_description
self._get_launch_description(self.__expanded_location)
File “/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_description_sources/any_launch_description_source.py”, line 53, in _get_launch_description
return get_launch_description_from_any_launch_file(location)
File “/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_description_sources/any_launch_file_utilities.py”, line 56, in get_launch_description_from_any_launch_file
raise InvalidLaunchFileError(extension, likely_errors=exceptions)
launch.invalid_launch_file_error.InvalidLaunchFileError: Caught exception when trying to load file of format [py]: name ‘launch_ros’ is not defined

Any idea what can I do with this issue?
Thank you!

Hi Tomas,

Is your ros2 launch command autocompleting with tab while you are typing it? If not, then it probably means that your package wasn’t configured properly.

I think you could try adding Node() without the preceding launch_ros.actions, and make sure the setup for your .launch.py is correct, following the instructions step by step.

1 Like