Import rclpy in scripted module

Hi there! I’m trying to import a python library called rclpy into a scripted module, the import is working in a regular python file (outside of Slicer) on Linux but I don’t think it’s supported by pip - I followed these build instructions: GitHub - ros2/rclpy: rclpy (ROS Client Library for Python)

I know this trick for importing python packages:
try:
import rclpy
except:
slicer.util.pip_install(‘rclpy’)
import rclpy

but is there a solution that doesn’t rely on pip?

Thanks in advance!

I ended up solving this by changed the PYTHON_ENV variable to my ROS2 python path!

Changing the PYTHON_ENV seemed to solve some issues but now I’m having an incompatibility problem between Python 3.8 in the rclpy and Python 3.9 in latest nightly Slicer - is there a way to change the python version if I build Slicer from source?

Changing python version is not something supported currently in Slicer. Things are tested and set up for Python 3.9. There would be a bit of work to get all the correct hashes for the whl files (matching Python 3.8) of all the c extension python packages included in Slicer core.

In terms of effort spent, you are probably better off setting your external environment to be Python 3.9 with rclpy to match what Slicer uses.