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

rosrun still use python2.7 instead of 3.8 inside noetic

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

nadim.arubai gravatar image

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

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
print(sys.version)
import yaml

by running the script with rosrun I got:

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

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 pytalker.py

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: https://github.com/tdenewiler/node_ex...

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
2

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

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

Comments

I opened a PR on that repo: https://github.com/tdenewiler/node_ex...

sloretz gravatar image sloretz  ( 2020-09-21 11:59:13 -0500 )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 -0500 )edit
0

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

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

Question Tools

Stats

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

Seen: 4,620 times

Last updated: Apr 23 '21