3.2.1 Create a Simple Publisher Node - can't launch the launch file for simple publisher

Concerning:
I am currently working on the “Understanding ROS2 Topics” chapter and I am stuck at part “3.2.1 Create a Simple Publisher Node”.

Issue:
I can’t start the launch file for this part.

Proceedure:
I have created my package, I made a script, I made a launch file and I made the changes in the setup.py file. I can use colcon build --packages-select publisher_pkg with the following output:

Starting >>> publisher_pkg
Finished <<< publisher_pkg [1.28s]

Summary: 1 package finished [1.49s]

When I try to start the launch file with ros2 launch publisher_pkg publisher_pkg_launch_file.launch.py, I get the following output:

[INFO] [launch]: All log files can be found below /home/user/.ros/log/2022-03-14-08-32-19-098299-2_xterm-3428
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [simple_publisher-1]: process started with pid [3430]
[simple_publisher-1] Traceback (most recent call last):
[simple_publisher-1]   File "/home/user/ros2_ws/install/publisher_pkg/lib/publisher_pkg/simple_publisher", line 11, in <module>
[simple_publisher-1]     load_entry_point('publisher-pkg==0.0.0', 'console_scripts', 'simple_publisher')()
[simple_publisher-1]   File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 490, in load_entry_point
[simple_publisher-1]     return get_distribution(dist).load_entry_point(group, name)
[simple_publisher-1]   File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2854, in load_entry_point
[simple_publisher-1]     return ep.load()
[simple_publisher-1]   File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2445, in load
[simple_publisher-1]     return self.resolve()
[simple_publisher-1]   File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2451, in resolve
[simple_publisher-1]     module = __import__(self.module_name, fromlist=['__name__'], level=0)
[simple_publisher-1] ModuleNotFoundError: No module named 'publisher_pkg.simple_publisher'
[ERROR] [simple_publisher-1]: process has died [pid 3430, exit code 1, cmd '/home/user/ros2_ws/install/publisher_pkg/lib/publisher_pkg/simple_publisher --ros-args'].

My Python script looks like this:

import rclpy
from rclpy.node import Node
from geometry_msgs.msg import Twist


class SimplePublisher(Node):

    def __init__(self):
        super().__init__('simple_publisher')
        self.publisher_ = self.create_publisher(Twist, '/cmd_vel', 10)
        timer_period = 0.5
        self.timer = self.create_timer(timer_period, self.timer_callback)

    def timer_callback(self):
        msg = Twist()
        msg.linear.x = 0.5
        msg.angular.z = 0.5
        self.publisher_.publish(msg)
        self.get_logger().info('Publishing: "%s"' % msg)


def main(args=None):
    rclpy.init(args=args)
    simple_publisher = SimplePublisher()
    rclpy.spin(simple_publisher)
    simple_publisher.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()

My launch file looks like this:

from launch import LaunchDescription
from launch_ros.actions import Node


def generate_launch_description():
    return LaunchDescription([
        Node(
            package='publisher_pkg',
            executable='simple_publisher',
            output='screen'),
    ])

My setup.py file looks like this:

from setuptools import setup
import os
from glob import glob

package_name = 'publisher_pkg'

setup(
    name=package_name,
    version='0.0.0',
    packages=[package_name],
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
        (os.path.join('share', package_name), glob('launch/*.launch.py'))
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='user',
    maintainer_email='user@todo.todo',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'simple_publisher = publisher_pkg.simple_publisher:main'
        ],
    },
)

Hello @dave.vandermeer1 ,

Where did you create the Python script? It should be created inside the publisher_pkg folder (which contains the __init__.py script).

Hello @albertoezquerro ,

My folder structure looks as follows:

grafik

I must admit that the folder structure of ROS2 is confusing to me as it seems less structured than in ROS1 in my opinion. If you have tops on that, I would be open to hear them.

Hello @dave.vandermeer1 ,

You have to place the Python script inside the publisher_pkg folder (which contains the __init__.py script).

Thanks a lot, this solved the issue. I am still too attched to ROS1!