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

How to declare an external python dependency in ros2

asked 2021-01-30 14:54:44 -0500

Loki Le Dev gravatar image

Hello, I am trying to understand what is the proper way of declaring an external python dependency in ros2-foxy.

What I want to do is create a simple python only ros2 package, and add some dependency to a python module in it, in my case it is tinymovr, that should be installed from pip.

So I create the package following this tutorial.

ros2 pkg create --build-type ament_python --node-name example_node example_python

Edit the main of the generated example_node.py file to import the dependency:

import tinymovr
def main():
    print('Hi from example_python.')
if __name__ == '__main__':
    main()

Now build, source and run the package:

colcon build
source install/local_setup.sh
ros2 run example_python example_node

Since I didn't specify anywhere the dependency, I get a nice ModuleNotFoundError

 File "~/ros2_ws/install/example_python/lib/python3.8/site-packages/example_python/example_node.py", line 1, in <module>
    import tinymovr
ModuleNotFoundError: No module named 'tinymovr'

So now where should I do it?


My first try is to edit the setup.py file and edit the install_requires field:

 install_requires=['setuptools', 'tinymovr'],

But when I build and run, there is still the ModuleNotFoundError.


I read that colcon is meant only for building, and I should use rosdep to install external dependencies. And if I understand correctly I should add some <depend> tag in the package.xml file. On ubuntu it can install system packages via apt, using a package not already installed for example:

<depend>python3-mypy</depend>

When I run

rosdep install --from-paths src --ignore-src -r -y

It nicely proposes to install it:

Continuing to install resolvable dependencies...
executing command [sudo -H apt-get install -y python3-mypy]

However I don't understand how to tell rosdep to install a package via pip. Maybe it is not supposed to do this. But why doesn't colcon install stuff from the setup.py file? I also tried with the setup.cfg file with no success.

Thank you for reading, adding examples for this case or mentioning it in the tutorial would be great.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-01-30 15:05:15 -0500

Loki Le Dev gravatar image

updated 2021-01-30 16:13:11 -0500

Ok after reading the rosdeptutorial I found the source for rosdep packages here: https://github.com/ros/rosdistro/blob...

I understand better now, also I found some explanations here: https://answers.ros.org/question/3235...

I can either submit a change to the public rosdep/python.yaml file. Or create some local file, and then update the sources.list of rosdep in my system to point to this file.

Tutorial

Let's create this file : custom-python.yaml install it on a system-wide path:

sudo mkdir -p /usr/local/share/ros && sudo cp hal-python.yaml /usr/local/share/ros

Edit the rosdep sources and update:

echo "yaml file:///usr/local/share/ros/hal-python.yaml" | sudo tee -a /etc/ros/rosdep/sources.list.d/20-default.list
rosdep update

Finally install dependencies for you package:

rosdep install --from-paths src --ignore-src -r -y
edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2021-01-30 14:54:44 -0500

Seen: 2,252 times

Last updated: Jan 30 '21