ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

How to use (python) virtual environments with ROS2?

asked 2021-02-04 16:19:47 -0500

jeferrandiz gravatar image

Hello, I am trying to run a python node which has imported some dependencies installed in a python virtual environment (using virtualenv). I have followed this tutorial:

The problem is that when I run the node, I have an import error ModuleNotFoundError, related to the packages installed in the virtual environment.

I have rclpy on the default system python. I do not know what further info I can add to help. Has any of you managed to work this out and have a nice python environment? Thank you for your help.

edit retag flag offensive close merge delete


Are you sure virtual env python activated? After the open a new console you have to set virtual env source. Also I tried with anaconda and it worked

bekirbostanci gravatar image bekirbostanci  ( 2021-02-04 16:30:05 -0500 )edit

Yes, I source the env as soon as I open a new console. I'll think to use anaconda too, thanks!

jeferrandiz gravatar image jeferrandiz  ( 2021-02-04 16:38:00 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2021-02-10 15:20:53 -0500

FabioArnez gravatar image


You need to check your $PYTHONPATH running echo $PYTHONPATH

This should show the path to your ROS distro, e.g. opt/ros/eloquent/lib/python3.6/site-packages, and the path to your environment packages .../path-to-your-env/lib/python3.6/site-packages.

If the env-package path is not present, add it using export PYTHONPATH='.../path-to-your-env/lib/python3.6/site-packages'

Run echo $PYTHONPATH again, check that your env-package path is there, and try to run your node ros2 run node...,... this should work.

To avoid adding the env-package path to $PYTHONPATH every single time, add export PYTHONPATH='.../path-to-your-env/lib/python3.6/site-packages' to your environment activate file.

Good luck!

edit flag offensive delete link more


Thank you so much!! It worked by adding the path, you've saved one life :-)

jeferrandiz gravatar image jeferrandiz  ( 2021-02-11 10:23:14 -0500 )edit

Question Tools



Asked: 2021-02-04 16:19:47 -0500

Seen: 6,030 times

Last updated: Feb 04 '21