ROS2 import Python modules
If I need to import let's say numpy in a ROS2 Python node, how does that work? I added it to package.xml but I'm getting getting "ModuleNotFoundError: No module named 'dynamixel_sdk'". Do I need to add it somewhere use? Should I use a venv?
Asked by infantasy on 2020-12-08 18:02:14 UTC
Answers
It's important to add all of your dependencies to the package.xml. That does a few different things:
- For users who are building your package from source, it lets
rosdep
know which packages to install from the system. - If you end up building your package on the ROS 2 buildfarm (https://build.ros2.org), it needs to be in there so that the dependencies are available when it is built.
- If you end up building your package on the ROS 2 buildfarm, it sets up the debian package dependencies properly so that
apt-get install ros-foxy-<mypackage>
installs all of the dependencies.
That being said, adding to the package.xml doesn't modify your environment at all. So it won't help your python package to "find" dependencies. Essentially if you can run python3
and import <dep>
, then your package should be able to find it. Typically those are either from system packages, or through pip.
Asked by clalancette on 2020-12-18 14:18:28 UTC
Comments
Sorry I'm still slightly confused. Just to make sure I understand... so in my case, I should add dynamixel_sdk to the package.xml... and since it is in package.xml... rosdep should be able to install it? I used pip to install the sdk, but I guess if someone else used rosdep, that would work?
Asked by infantasy on 2020-12-18 14:30:14 UTC
The general idea with listing your dependencies in the package.xml is so that your users can just use rosdep
to install the dependencies. At least for system packages (ones you can install with apt install
), that's how it works. I'm not quite sure how pip
packages work there, but I think it works in a similar way.
Asked by clalancette on 2021-01-04 14:57:53 UTC
Comments
Btw, this question was asked here as well but didn't receive an answer: https://answers.ros.org/question/365628/ros2-import-regular-python-modules-from-where/
Asked by infantasy on 2020-12-08 18:04:51 UTC
Just following up... It doesn't seem like it did anything to add it to package.xml, although I saw this in some Github repos. I think I will just use a venv / requirements.txt, although I'm not sure that this is the best approach.
Asked by infantasy on 2020-12-09 01:58:02 UTC
We spent a little time documenting this a bit more, maybe this will help: https://index.ros.org/doc/ros2/Tutorials/Using-Python-Packages/
Asked by mjcarroll on 2020-12-22 13:31:19 UTC
Thanks so much for the documentation!! I'm guessing you meant to link to https://index.ros.org/doc/ros2/Tutorials/Using-Python-Packages/
Asked by infantasy on 2020-12-22 13:33:41 UTC