shebang is ignored by rosrun
I am trying to use a specific python interpreter to run a ros node written by python. For example, I created a package named tmp, and there is a python code named tmp_new.py in catkin_ws/src/tmp/scripts. The content of tmp_new.py is:
#!/usr/bin/python2
import sys
print(sys.executable)
I have edited CMakeLists.txt so that the python executable file can be found:
catkin_install_python(PROGRAMS scripts/tmp_new.py DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} )
After then, I run catkin_make to build the package, source devel/setup.bash to set the environment, and rosrun tmp tmp_new.py to execute the toy python file.
However, the shebang is completely ignored, and the output is /usr/bin/python3.
To rule out the possibility that my shebang is incorrect, I go into catkin_ws/src/tmp/scripts and run ./tmp_new.py and the shebang is recognized and the output of the program is /usr/bin/python2, which conforms to my expectation.
I am quite confused why this happened? It seems the shebang is completely ignored by rosrun. I also tried to write a launch file, and the behavior is the same as rosrun.
(Some more information: The above is just a toy example which explains the problem I encountered easily. My final goal is to ask ros to execute a python program with a specific virtualenv environment. For example, I created a virtual environment with virtualenv venv, and I set the shebang of the python program to #!/home/xxxx/catkin_ws/venv/bin/python. Rosrun always calls /usr/bin/python3 instead of the virtual environment.)
Environment:
- ROS version: Noetic 1.15.11
- Ubuntu 20.04 x86_64, kernel 5.10.9-051009-generic
- My default python interpreter is /usr/bin/python3
- env | grep ROS > ROS_VERSION=1 ROS_PYTHON_VERSION=3 ROS_PACKAGE_PATH=/home/xxxx/catkin_ws_jetson/src:/home/xxxx/catkin_ws/src:/opt/ros/noetic/share ROSLISP_PACKAGE_DIRECTORIES=/home/xxxx/catkin_ws_jetson/devel/share/common-lisp:/home/xxxx/catkin_ws/devel/share/common-lisp ROS_ETC_DIR=/opt/ros/noetic/etc/ros ROS_MASTER_URI=http://localhost:11311 ROS_ROOT=/opt/ros/noetic/share/ros ROS_DISTRO=noetic
Well, I found a solution by myself. If I don't edit the CMakeLists.txt file of the packge as I said in the question, i.e.
and delete the build and devel folders, and run catkin_make again.
rosrun tmp tmp_new.py will use the shebang and call the python2 interpreter.
However, I still don't know why this happen? Anybody can explain this? Thanks!