ros2 run ignores virtualenv

asked 2023-04-03 15:29:44 -0500

danbrooks gravatar image

I am trying to run a python node on ros2/foxy that depends on software installed in a virtualenv. I've been following these directions:

After activating the virtual environment, running colcon build, and sourcing the workspace, I can...

  • run the python3 interpreter and import both my ROS modules and virtualenv modules successfully, and
  • call my script directly (e.g. python3 mydemo/mydemo/ and run it successfully (including importing the virtualenv modules).

However, when I try to run the script using ros2 run, the pythonpath seems to get changed and I can no longer find my virtualenv modules.

How to I get ros2 run to find my virtualenv modules?

Here is a minimal example of the issue I'm encountering:

Setup a workspace and python package

source /opt/ros/foxy/setup.bash
mkdir -p test-ws/src
cd test-ws/src
ros2 pkg create --build-type ament_python mydemo

Add a script vim mydemo/mydemo/

import rclpy
from rclpy.node import Node
from std_msgs.msg import String

import sys

import jinja2

def main():

if __name__ == "__main__":

Add rclpy and std_msgs to package.xml


Add an entrypoint to

entry_points={'console_scripts': ['tryme=mydemo.tryme:main']}

Now, following the ros2 documentation on how to setup a virtual environment, set up one and install a dependency (for example, jinja2) and activate it.

cd test-ws/src
python3 -m venv venv --system-site-packages --symlinks
source ./venv/bin/activate
touch ./venv/COLCON_IGNORE
python3 -m pip install jinja2

build the workspace and source it

cd test-ws
colcon build --symlink-install
source install/setup.bash

Check the python path

(venv) root@3ad26851ff20:/app/test-ws# echo $PYTHONPATH

Try to use ros2 run to run the node. This will fail because it cannot find the virtualenv dependency we installed (which we can also see is no longer in sys.path.

(venv) root@3ad26851ff20:/app/test-ws# ros2 run mydemo tryme
['/app/test-ws/install/mydemo/lib/mydemo', '/app/test-ws/build/mydemo', '/app/test-ws/install/mydemo/lib/python3.8/site-packages', '/opt/ros/foxy/lib/python3.8/site-packages', '/usr/lib/', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages']
Traceback (most recent call last):
  File "/app/test-ws/install/mydemo/lib/mydemo/tryme", line 11, in <module>
    load_entry_point('mydemo', 'console_scripts', 'tryme')()
  File "/usr/lib/python3/dist-packages/pkg_resources/", line 490, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python3/dist-packages/pkg_resources/", line 2854, in load_entry_point
    return ep.load()
  File "/usr/lib/python3/dist-packages/pkg_resources/", line 2445, in load
    return self.resolve()
  File "/usr/lib/python3/dist-packages/pkg_resources/", line 2451, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/app/test-ws/build/mydemo/mydemo/", line 8, in <module>
    import jinja2
ModuleNotFoundError: No module named 'jinja2 ...
edit retag flag offensive close merge delete


Hi. I'm having the same problem as you. Did you fix it?

longhuu3011 gravatar image longhuu3011  ( 2023-06-26 02:34:56 -0500 )edit

Hi there, I think I encountered the same issue at some point and I can't really remember how I solved it but I think the symlink option on the system site packages when creating the virtual env did the job for me,

python3 -m venv <env_name>  --system-site-packages --symlinks

The solution came from this thread: However this problem still seems to be an open issue.

pierreC gravatar image pierreC  ( 2023-06-26 11:18:23 -0500 )edit

Hi . I'm afraid to change the symlink because it might break the packages . I really can't find a solution :(

longhuu3011 gravatar image longhuu3011  ( 2023-06-27 21:27:50 -0500 )edit