How to declare an external python dependency in ros2
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.