Ask Your Question

rosrun still use python2.7 instead of 3.8 inside noetic

asked 2020-09-21 02:56:06 -0600

nadim.arubai gravatar image

updated 2021-04-23 21:13:40 -0600

130s gravatar image

I want to use rosrun or roslaunch to run some python node in ros noetic. Although the examples I tested worked in kinetic under ubuntu 16.04, they no longer work in noetic under ubuntu 20.04. The main problem that rosrun failed to locate yaml module, while calling the python script directly work. After some checking, and trying small examples, I found the problem by putting

import sys
import yaml

by running the script with rosrun I got:

nnn@aaa:~/ros/catkin_ws/src/node_example-main/src$ rosrun node_example

2.7.18rc1 (default, Apr  7 2020, 12:05:55) 
[GCC 9.3.0]

by running it with default system python:

nnn@aaa:~/ros/catkin_ws/src/node_example-main/src$ python3

3.8.2 (default, Jul 16 2020, 14:00:26) 
[GCC 9.3.0]

I tried many solution but all of them have failed.

for reference I used this example from ROS:

and "catkin_make install" build successfully with out errors or warnings.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2020-09-21 11:48:24 -0600

sloretz gravatar image

It looks like the problem is the example is using install(PROGRAMS ... for Python scripts, and the Python scripts have shebangs that say #!/usr/bin/env python. When the scripts are installed the shebangs are unchanged, so it uses whatever version of python the python command starts.

To fix this, the code should be using catkin_install_python(...) which rewrites the shebang's to point to the correct version (Python 2 on ROS Melodic and earlier, Python 3 on ROS Noetic).

edit flag offensive delete link more


I opened a PR on that repo:

sloretz gravatar image sloretz  ( 2020-09-21 11:59:13 -0600 )edit

Thanks, using catkin_install_python(...) is more elegant and efficient than removing python2 or manually force python3 to be the default.

nadim.arubai gravatar image nadim.arubai  ( 2020-09-27 02:47:25 -0600 )edit

answered 2020-09-21 04:19:45 -0600

nadim.arubai gravatar image

I found the the cause is ROS2. After installing ROS2 foxy, it also install python2.7 packages, and somehow make ROS1 use it.

to solve the issu simply remove python2.7, and force python to be python3 by:

sudo apt-get remove python2 sudo apt-get autoremove sudo apt-get install python-is-python3

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2020-09-21 02:56:06 -0600

Seen: 1,593 times

Last updated: Apr 23