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

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

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

Rayman gravatar image

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

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

1 Answer

Sort by » oldest newest most voted
1

answered 2019-10-07 09:00:37 -0600

mgruhler gravatar image

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.

edit flag offensive delete link more

Comments

PR https://github.com/ros/catkin/pull/1044 greatly improves working with python2/3 due to automatically rewriting the shebang to the wanted python version. Now it's possible to follow the wiki.

Rayman gravatar image Rayman  ( 2020-07-03 06:55:56 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2019-10-04 12:58:29 -0600

Seen: 855 times

Last updated: Oct 09 '19