Module has no attribute main error

Hi,

I am new to this site and I was taking rosbasic course and decided to tinker on my own outside your online environment. I am getting a error that I can’t find any where on net. I tried everything as I am expected but I could be wrong.
Here’s the script move_turtle.py

import rclpy
import sys
from rclpy.node import Node
from geometry_msgs.msg import Twist, Quaternion

class move_turtle(Node):
      def __init__(self):
            super().__init__('move_turtle')
            self.publisher_Quaternion = self.create_publisher(Quaternion, 'cmd_vel', 10)
            self.publisher_Twist = 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_quaternion = Quaternion()
            #msg_twist = Twist()
            msg_quaternion.x = 0.0
            msg_quaternion.y = 0.0
            msg_quaternion.z = 1.0
            msg_quaternion.w = 0.0

            self.publisher_Quaternion.publish(msg_quaternion)
            self.get_logger().info(f"Publishing {msg_quaternion} to {self.publisher_Quaternion}")

      def publish(self):
            pass
      def move(self):
            pass

      def main(args=None):
            rclpy.init(args=args) 
            move = move_turtle()
            rclpy.spin(move)
            move.destroy_node()
            rclpy.shutdown()

      if __name__ == "__main__":
            sys.exit(main())

here’s the launch file move_turtle_launch_file.launch.py

from launch import LaunchDescription
from launch_ros.actions import Node

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

and the setup.py

from setuptools import find_packages, setup
import os
from glob import glob

package_name = 'move_turtle'

setup(
    name=package_name,
    version='0.0.0',
    packages=find_packages(exclude=['test']),
    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='diysumit',
    maintainer_email='diysumit@gmail.com',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'move_turtle = move_turtle.move_turtle:main'
        ],
    },
)

I followed the exact way I have been taught to run these, so I first created the package
ros2 pkg create --build-type ament_python move_turtle --dependencies geometry_msgs std_msgs rclpy
Then I created all those file and gave executable permission to launch file and python script using chmod 777
and then finally sources the local setup.zsh and ran I got this error, I also tried doing the overlay and underlay thing, like running this in two different terminals but same error.

Hi,

I think, “def main” should be in the beginning of the line.

I mean, “pull” the main method and the line starting with “if” to the left side.
Now both are “in” the class. They should be standalone.

2 Likes

Thanks so much sometimes I make stupid mistakes like this, it finally ran!

Your welcome.
I do not think that is your fault.
I rather think, this is the stupidity of Python. It is ridiculously affected by whitespace characters,

Well, I wouldn’t call that “stupidity.” It’s one of the nuances of the language. Indentation is the equivalent of braces in other languages, for separating code blocks.

Every language has its nuances. If we must use them, we just need to understand them.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.