Ask Your Question
1

How to use (python) virtual environments with ROS2?

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

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: https://index.ros.org/doc/ros2/Tutori...

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

Comments

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 -0600 )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 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
2

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

FabioArnez gravatar image

Hi,

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

Comments

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 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

3 followers

Stats

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

Seen: 806 times

Last updated: Feb 04