1
$\begingroup$

Could anyone please share a current link on the successful use of running an ROS2 node (>= Humble) in a venv with a different python version?

The troubles ...

My ROS2 system is on an RPi and Ubuntu 22.04 installed with the arm64 raspy image. Ubuntu's system python version is 3.10.

I need to create an ROS2 package for a Coral device which uses pycoral, which requires python <=3.9. Ubuntu 22.04 uses python3.10.

To test this I'm using the base Ubuntu install and only adding python3.9 through apt-get.

I installed ROS2 Humble using the Debian packages (not from source).

I installed the simple published example from https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Py-Publisher-And-Subscriber.html and that builds and runs fine.

I then added "import pycoral" to the publisher_member_function.py, built it, sourced the install dir and ran it and as expected the module could not be imported.

I then created a venv named venv-3.9.2

python3.9 -m venv venv-3.9.2 --system-site-packages --symlinks

Note - I also tried this without the extras:

python3.9 -m venv venv-3.9.1

Then activated that and added pycoral using python -m pip install pycoral. I ran a test.py with import pycoral and the import succeeded.

Still in the venv, I ran the talker

ros2 run py_pubsub talker

Again, the pycoral module could not be found, but the log indicates the script is looking in /usr/lib/python3.10.

I also tried adding this to setup.cfg and rebuilding:

[build_scripts]
 executable = /home/mark/venv-3.9.2/bin/python

Running this after build creates an error including:

ModuleNotFoundError: No module named 'rclpy._rclpy_pybind11'

I also tried the order of building and activating the venv and installing colcon in the venv per https://github.com/ros2/ros2/issues/1094 - still no luck.

This seems to be a common issue but I can't find a clear example on how to successfully do this.

$\endgroup$

2 Answers 2

1
$\begingroup$

In general, it is fine to use slightly different versions of python then the default on the platform. However, be aware you may need to patch or fix things depending on what Python version you choose.

However, you cannot use the precompiled binaries with those. You will have to install from source with the virtual environment activated anytime that you build and anytime you use it. If you use different python interpreters at different times in a workspace, you're going to get lots of errors like in your question.

This is how core developers test ahead. It works, but you have to be very careful.

$\endgroup$
0
$\begingroup$

No, you can not successfully run a different version of python if you import the ros libraries. There is a solution, but it takes a little extra programming effort.

You can create a non-ros application that runs outside of ros, but has two-way communication with the ros system. The ros1/ros2 package rosbridge_suite provides a ros node that is also a websocket server. Typically you won't have to do any additional programming for the ros side.

Your app then implements a websocket client. The websocket server provides a (non-ros) API that allows your client to publish or subscribe ros messages through the websocket server. Note that the transfer is not efficient, so you shouldn't plan to transfer a large amount of data this way. Also, there are python libraries like roslibpy to help you implement your client.

$\endgroup$
1
  • 1
    $\begingroup$ Excellent response. After spending a week or so on this issue, I ended up dropping down to Ubuntu 20.04 for RPi which uses python3.8 and not using venv at all. My use case is object detection using a Coral USB device which requires an image frame and responds with a bbox, object class and position coordinates within the frame. Because it will need the frame data (and just every nth frame for a response with a second or two), probably a python based web socket would do the trick if it turns out I need something more current that Ubuntu 20.04. $\endgroup$
    – Thou Maker
    Commented Oct 6, 2023 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.