ros2 run ImportError
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'
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.Thank you for the advice! I write if
__name__ == '__main__':
inmy_module
.Error message when my_module running and script enter code here add in a question.