What controls the Python search path, and how?
In my shell I first activate a Python virtual environment and then the Catkin workspace which results in this sys.path
, which searches for Python modules in this order.
[
'/home/user/project/devel/lib/python3/dist-packages',
'/opt/ros/noetic/lib/python3/dist-packages',
'/home/user/project/.venv/lib/python37.zip',
'/home/user/project/.venv/lib/python3.7',
'/home/user/project/.venv/lib/python3.7/lib-dynload',
'/usr/lib/python3.7',
'/home/user/project/.venv/lib/python3.7/site-packages',
'/home/user/.local/lib/python3.7/site-packages',
'/usr/local/lib/python3.7/dist-packages',
'/usr/lib/python3/dist-packages'
]
Yet when I run roslaunch
from this shell, which in turn executes a Python node, this is the sys.path
it sees. My virtual environment is no longer active.
[
'/home/user/project/src/pkg/src',
'/home/user/project/devel/.private/pkg/lib/pkg',
'/home/user/project/devel/lib/python3/dist-packages',
'/opt/ros/noetic/lib/python3/dist-packages',
'/usr/lib/python37.zip',
'/usr/lib/python3.7',
'/usr/lib/python3.7/lib-dynload',
'/home/user/.local/lib/python3.7/site-packages',
'/usr/local/lib/python3.7/dist-packages',
'/usr/lib/python3/dist-packages'
]
I would expect it to add the first two entries (so that the ROS package's own Python modules take priority), but keep the rest which include my .venv
directories.
It appears that it is due to the Python interpreter
/usr/bin/python3
being executed instead of/home/user/project/.venv/bin/python3
even though the shebang line is#!/usr/bin/env python3
and thePATH
(as the node sees it) should prioritize the one from my virtual environment.When
roslaunch
callsexecve()
to run my node, it is executing (via some symlinks)/home/user/project/devel/.private/pkg/lib/pkg/node
which is a script that goes:Which rather than just execute the program as any other executable, reads the entire thing and then executes it...