ros2 run ignores virtualenv
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:
https://docs.ros.org/en/foxy/How-To-G...
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/tryme.py
) 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/tryme.py
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
import sys
print(sys.path)
import jinja2
def main():
print("here")
if __name__ == "__main__":
main()
Add rclpy
and std_msgs
to package.xml
<depend>rclpy</depend>
<depend>std_msgs</depend>
Add an entrypoint to setup.py
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
/app/test-ws/build/mydemo:/app/test-ws/install/mydemo/lib/python3.8/site-packages:/opt/ros/foxy/lib/python3.8/site-packages
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/python38.zip', '/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/__init__.py", line 490, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2854, in load_entry_point
return ep.load()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2445, in load
return self.resolve()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2451, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/app/test-ws/build/mydemo/mydemo/tryme.py", line 8, in <module>
import jinja2
ModuleNotFoundError: No module named 'jinja2 ...
Hi. I'm having the same problem as you. Did you fix it?
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,
The solution came from this thread: https://github.com/ros2/ros2/issues/1... However this problem still seems to be an open issue.
Hi . I'm afraid to change the symlink because it might break the packages . I really can't find a solution :(