Rosanswers logo

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: https://index.ros.org/doc/ros2/Tutorials/Using-Python-Packages/#installing-via-a-virtual-environment

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.

Originally posted by jeferrandiz on ROS Answers with karma: 27 on 2021-02-04

Post score: 2

Original comments

Comment by bekirbostanci on 2021-02-04:
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

Comment by jeferrandiz on 2021-02-04:
Yes, I source the env as soon as I open a new console. I'll think to use anaconda too, thanks!


1 Answer 1


Rosanswers logo


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!

Originally posted by FabioArnez with karma: 76 on 2021-02-10

This answer was ACCEPTED on the original site

Post score: 6

Original comments

Comment by jeferrandiz on 2021-02-11:
Thank you so much!! It worked by adding the path, you've saved one life :-)


Your Answer

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