Using rosrun within virtual environment

asked 2020-12-11 09:10:41 -0600

JayDe gravatar image

updated 2020-12-14 01:22:28 -0600

Hi! I am trying to run a ros node within a python virtual environment, a procedure which I believe has been working before with ubuntu16.04 and kinetic. To do so, I am following these steps:

  1. Creating a virtual environment (python3 -m venv myEnv)
  2. Installing a required python package, e.g. torch into myEnv
  3. Souring the virtual environment (source myEnv/bin/activate)
  4. Appending to PYTHONPATH by using export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python3.8/dist-packages:/usr/lib/python3/dist-packages, as otherwise certain imports won't work (e.g. import cv_bridge will fail with an unreported exception.
  5. Executing rosrun mypackage mynode

This will fail saying that torch is not available. It turns out the script mynode is not executed within the virtual environment. When running the mynode script directly, torch will be found. Note, that the node has /usr/bin/env python3 written in the first line. When I execute rosrun --prefix "/usr/bin/env python3" mypackage mynode it will work.... Thanks!

edit retag flag offensive close merge delete