ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ros2 run ImportError

asked 2018-11-08 02:54:42 -0600

jetman gravatar image

updated 2018-11-08 19:32:18 -0600

Hi! I am working with ROS2 Bouncy, using colcon to build.

ROS2 build is successful. I create new package using Python3.5 I write setup.py, package.xml,setup.cfg and colcon build successful.But I run the package using ros2 run, Importerror: module 'module name' has no attribute 'main'.

Package referred ROS2 examples/rclpy/topic/mimimal_publisher

setup.py is:

from setuptools import setup

package_name = 'test_my_module'

setup(
    name=package_name,
    version='0.0.1',
    packages=[],
    py_modules=[
        'my_module'],
    install_requires=['setuptools'],
    zip_safe=True,
    author='name',
    author_email='mail',
    maintainer='name',
    maintainer_email='mail',
    keywords=['ROS'],
    classifiers=[
        'Intended Audience :: Developers',
        'License :: OSI Approved :: Apache Software License',
        'Programming Language :: Python',
        'Topic :: Software Development',
    ],
    description='test my module.',
    license='Apache License, Version 2.0',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'my_module = my_module:main',
        ],
    },
)

my_module is:

import rclpy
from rclpy.node import Node
from std_msgs.msg import String


class TestPulisher(Node):

    def __init__(self):
        super().__init__('test_pubrisher')
        self.publisher1 = self.create_publisher(String, 'topic1')      
        time_period = 0.5
        self.timer = self.create_timer(timer_period,self.timer_callback)
        self.i = 0

    def timer_callback(self):
        msg1 = String()
        msg1.data = 'Hello World: %d' % self.i
        self.publisher1_.publish(msg1)
        self.get_logger().info('Publisher1: "%s"' % msg1.data)
        self.i += 1

    def main(args=None):
        rclpy.init(args=args)
        test_publisher = TestPulisher()
        rclpy.spin(test_publisher)

if __name__ == '__main__':
    main()

Error message when running my_module:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/pkg_resources/__init__.py", line 2351, in resolve
    return functools.reduce(getattr, self.attrs, module)
AttributeError: module 'my_module' has no attribute 'main'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/main/ros2_ws/install/test_my_module/lib/test_my_module/my_module", line 11, in <module>
    load_entry_point('test-my-module', 'console_scripts', 'my_module')()
  File "/usr/local/lib/python3.5/dist-packages/pkg_resources/__init__.py", line 484, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/local/lib/python3.5/dist-packages/pkg_resources/__init__.py", line 2725, in load_entry_point
    return ep.load()
  File "/usr/local/lib/python3.5/dist-packages/pkg_resources/__init__.py", line 2343, in load
    return self.resolve()
  File "/usr/local/lib/python3.5/dist-packages/pkg_resources/__init__.py", line 2353, in resolve
    raise ImportError(str(exc))
ImportError: module 'my_module' has no attribute 'main'
edit retag flag offensive close merge delete

Comments

Please post the exact command you ran. Based on the error message, I suspect you are missing the main entrypoint in my_module. For example.

jacobperron gravatar image jacobperron  ( 2018-11-08 17:57:18 -0600 )edit

Thank you for the advice! I write if __name__ == '__main__': in my_module.
Error message when my_module running and script enter code here add in a question.

jetman gravatar image jetman  ( 2018-11-08 19:30:41 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2018-11-08 20:11:11 -0600

Dirk Thomas gravatar image

You Python module doesn't have a main function. It only contains a method with that name and handles __main__ which is not the same.

So if your entry point uses my_module = my_module:main you need to provide def main(...): ... function in the my_module module.

edit flag offensive delete link more

Comments

Thank you for answering! A little mistake of python script. def main fixed,it works

jetman gravatar image jetman  ( 2018-11-08 21:07:11 -0600 )edit

Question Tools

Stats

Asked: 2018-11-08 02:54:42 -0600

Seen: 2,375 times

Last updated: Nov 08 '18