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

shebang is ignored by rosrun

asked 2021-07-19 17:49:16 -0600

zptang gravatar image

updated 2021-07-20 06:33:07 -0600

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 in catkin_ws/src/tmp/scripts. The content of is:

import sys

I have edited CMakeLists.txt so that the python executable file can be found:


After then, I run catkin_make to build the package, source devel/setup.bash to set the environment, and rosrun tmp 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 ./ 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.)


  • 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
edit retag flag offensive close merge delete


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 will use the shebang and call the python2 interpreter.

However, I still don't know why this happen? Anybody can explain this? Thanks!

zptang gravatar image zptang  ( 2021-07-20 14:06:22 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2021-07-25 12:13:22 -0600

fergs gravatar image

The catkin_install_python directive automatically rewrites the shebang on your file to match the Python version specified by the ROS version (since you are on Noetic, ROS_PYTHON_VERSION is set to 3).

If you really want to keep the shebang setup for Python 2, you would want to use a plain old install directive in CMake would would not do the extra shebang rewrite.

edit flag offensive delete link more


Thanks for your answer!

zptang gravatar image zptang  ( 2021-07-27 14:19:22 -0600 )edit

Thanks, please click the checkmark next to the answer to accept it so this gets marked as answered.

fergs gravatar image fergs  ( 2021-09-27 14:42:33 -0600 )edit


zptang gravatar image zptang  ( 2021-09-27 14:46:44 -0600 )edit

Question Tools

1 follower


Asked: 2021-07-19 17:43:34 -0600

Seen: 401 times

Last updated: Jul 25 '21