How to create a python script that works with python3 and 2

asked 2019-10-04 12:58:29 -0500

Rayman gravatar image

updated 2019-10-09 04:15:12 -0500

In the install space, python scripts have their shebang changed to the version specified with PYTHON_EXECUTABLE. In the devel workspace this also happens for globally installed scripts. But when I run a normal script with rosrun, it will take the version specified in the shebang. __How can I create a script that works in both python versions in the devel space?__

I could change the shebang to python3 but then it's not backwards compatible. I would like to have this handled automatically.

Is this a good solution?

#!/usr/bin/env python$ROS_PYTHON_VERSION

/usr/bin/env: ‘python$ROS_PYTHON_VERSION’: No such file or directory

EDITI found that REP-151 will describe the path for switching from Python 2 to Python 3 in ROS 1. It's still WIP, but I found some answers there. https://github.com/ros-infrastructure...

edit retag flag offensive close merge delete

Comments

There is a documentation about UsingPython3 in the ROS wiki, with one specific section towards changing the shebang.

Does that help?

P.S: This documentation has been announced recently on discourse and as they say:

While it has had a few reviewers, I think this is the first time it’s been linked publicly, so if you have a wiki account please fix any issues you find.

mgruhler gravatar imagemgruhler ( 2019-10-07 09:00:37 -0500 )edit