Unit4 - Question about entry_points in setup.py

Hi, I have a question.

In Chapter 4.7, I see this code in setup.py file

‘movement_server = movement_pkg.movement_server:main’,
‘movement_client = movement_pkg.movement_client:main’

then I can run .py file by:
ros2 run movement_pkg movement_client “Turn Left”

But if I change to:
‘movement_server = movement_pkg.movement_server:main’

I can not run .py file. I got the error:
Traceback (most recent call last):
File “/home/user/ros2_ws/install/movement_pkg/lib/movement_pkg/movement_client”, line 33, in
sys.exit(load_entry_point(‘movement-pkg==0.0.0’, ‘console_scripts’, ‘movement_client’)())
File “/home/user/ros2_ws/install/movement_pkg/lib/movement_pkg/movement_client”, line 25, in importlib_load_entry_point
return next(matches).load()
StopIteration
[ros2run]: Process exited with failure 1

My question is why can I not run a .py file normally by ros2 run <.py file> normally?

Thanks

Hello @NguyenDuyDuc,

sorry but it is not clear to me what has changed:

For first posted this:

And then this:

So do you mean that you removed this line ‘movement_client = movement_pkg.movement_client:main’ or what exactly is the change you made to your setup.py file?

If you indeed removed the second line, then the reason that

ros2 run movement_pkg movement_client

will not run is that you have not declared movement_client as entry point. When you declare an entry point the python file is copied to a location the ros2 run can find it. If you don’t declare the entry point ros2 run will not be able to find that file.

Cheers,

Roberto

  1. My changing is removing this line: ‘movement_client = movement_pkg.movement_client:main’
  2. My thought is setup.py used for running .launch.py file. Follow your explanation, setup.py file used for running .py file also?

Hi @NguyenDuyDuc ,

I think I understand what you are trying to ask.

Yes, you cannot run a python file with ros2 run <pkg_name> <script.py> in ROS2, unlike in ROS1 because: ROS2 follows “Node Inheritance”. So for your Node to start running it must be defined in the setup.py file under entry_points.

In ROS1, you would just import rospy and initialize a node in the main function under which you will instantiate your program class. Your program class in ROS1 does not inherit from a base Node Class.

But in ROS2, every class that you define are mostly inherited from base Node Class. Therefore, to initialize the execution of the base Node Class, you must have the program listed under inside setup.py file to recognize your file as an executable for ROS2 execution.

No. setup.py file informs the ROS2 framework about the scripts that are executable inside the package.
You need to modify setup.py file to do ros2 run ... and to launch the launch files containing the reference to executables.

Yes. So, if you are using either ros2 run ... or ros2 launch ... command, you MUST modify setup.py file.

I hope I clarified your doubts (and I also believe that I am correct with my explanation).

Regards,
Girish

2 Likes

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