# Import rclpy in scripted module

**URL:** https://discourse.slicer.org/t/import-rclpy-in-scripted-module/22249
**Category:** Support
**Tags:** python, scripted, robotics
**Created:** [March 1, 2022, 8:10pm UTC](https://discourse.slicer.org/t/import-rclpy-in-scripted-module/22249 "2022-03-01T20:10:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![LauraConnolly](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@LauraConnolly](https://discourse.slicer.org/u/LauraConnolly)
#### Post date: [March 1, 2022, 8:10pm UTC](https://discourse.slicer.org/t/import-rclpy-in-scripted-module/22249/1 "2022-03-01T20:10:22Z")

</div>

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)](https://github.com/ros2/rclpy)

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!

---

<div class="post-metadata">

### Author: ![LauraConnolly](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@LauraConnolly](https://discourse.slicer.org/u/LauraConnolly)
#### Post date: [March 3, 2022, 1:46pm UTC](https://discourse.slicer.org/t/import-rclpy-in-scripted-module/22249/2 "2022-03-03T13:46:31Z")

</div>

I ended up solving this by changed the PYTHON\_ENV variable to my ROS2 python path!

---

<div class="post-metadata">

### Author: ![LauraConnolly](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@LauraConnolly](https://discourse.slicer.org/u/LauraConnolly)
#### Post date: [March 3, 2022, 6:09pm UTC](https://discourse.slicer.org/t/import-rclpy-in-scripted-module/22249/3 "2022-03-03T18:09:38Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)
#### Post date: [March 3, 2022, 6:43pm UTC](https://discourse.slicer.org/t/import-rclpy-in-scripted-module/22249/4 "2022-03-03T18:43:43Z")

</div>

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.
