ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

What controls the Python search path, and how?

asked 2022-04-21 14:17:43 -0600

rgov gravatar image

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.

edit retag flag offensive close merge delete

Comments

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 the PATH (as the node sees it) should prioritize the one from my virtual environment.

rgov gravatar image rgov  ( 2022-04-21 14:32:59 -0600 )edit

When roslaunch calls execve() 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:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# generated from catkin/cmake/template/script.py.in

Which rather than just execute the program as any other executable, reads the entire thing and then executes it...

rgov gravatar image rgov  ( 2022-04-21 14:49:42 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-04-21 15:03:28 -0600

rgov gravatar image

Basically, my nodes were being launched with a different python3 executable than I expected.

This is because roslaunch actually launches a trampoline script, based on this template, that provides a different shebang line which is fixed at build time.

(Aside: This seems like very unusual behavior from Catkin. Why not just write out a file with #!/whatever/python3 pathtofile.py instead of this? Why the circuitousness?)

To get the right shebang line in this trampoline, the virtual environment needs to be active while building.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2022-04-21 14:17:43 -0600

Seen: 157 times

Last updated: Apr 21 '22