0
$\begingroup$

Rosanswers logo

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


Originally posted by zptang on ROS Answers with karma: 23 on 2021-07-19

Post score: 2


Original comments

Comment by zptang on 2021-07-20:
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.

# catkin_install_python(PROGRAMS scripts/tmp_new.py DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} )

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!

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by fergs with karma: 13902 on 2021-07-25

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by zptang on 2021-07-27:
Thanks for your answer!

Comment by fergs on 2021-09-27:
Thanks, please click the checkmark next to the answer to accept it so this gets marked as answered.

Comment by zptang on 2021-09-27:
Done!!!!!!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.