0
$\begingroup$

Rosanswers logo

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_example

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


Originally posted by nadim.arubai on ROS Answers with karma: 37 on 2020-09-21

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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).


Originally posted by sloretz with karma: 3061 on 2020-09-21

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by sloretz on 2020-09-21:
I opened a PR on that repo: https://github.com/tdenewiler/node_example/pull/43

Comment by nadim.arubai on 2020-09-27:
Thanks, using catkin_install_python(...) is more elegant and efficient than removing python2 or manually force python3 to be the default.

$\endgroup$

Your Answer

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