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

how to define ROS kinetic to use python3 instead of python2.7?

asked 2016-06-21 02:17:10 -0600

SHPOWER gravatar image

Hey,

I want to use Qt5 and python3 in ROS. I read that ROS kinetic should support python3. However I haven't found any instruction about the proper way to configure PYTHONPATH, CATKIN, etc. Is there any proper way that I can do that without loosing ROS functionality?

Thanks in advance

edit retag flag offensive close merge delete

Comments

1

Hello, did you happen to find any way to get ROS to work with Python3?

Tanmay gravatar image Tanmay  ( 2016-10-03 15:02:00 -0600 )edit
1

Were you able to get ROS running for python3?

zishan gravatar image zishan  ( 2017-12-20 03:46:27 -0600 )edit

4 Answers

Sort by ยป oldest newest most voted
16

answered 2016-06-21 10:03:13 -0600

Dirk Thomas gravatar image

updated 2019-08-27 11:43:14 -0600

No ROS distribution officially supports Python 3. ROS Kinetic uses Qt 5 though, e.g. for rqt and rviz.

If you want to try it anyway you need to build everything from source (following the "normal" from source instructions). But before you will need to install all Python dependencies (e.g. catkin_pkg) for Python 3. Since the Debian packages are not side-by-side installable with the Python 2 version using a virtualenv will likely be the easiest way. The virtualenv will also ensure that python is pointing to a Python 3 interpreter. Otherwise you would need to pass a custom interpreter to the build (like -DPYTHON_EXECUTABLE=python3).

Anyway if you go that route you should expect problems and will find things which simply don't work with Python 3.

Update:

things are changing with the Noetic release coming up in less than a year which does target Python 3. Please see the latest instructions in the wiki about the topic: http://wiki.ros.org/UsingPython3

edit flag offensive delete link more

Comments

thank you.

SHPOWER gravatar image SHPOWER  ( 2016-06-22 09:06:55 -0600 )edit

Please mark the answer as accepted if it answered your question so that others find it now easily.

Dirk Thomas gravatar image Dirk Thomas  ( 2016-06-22 09:36:50 -0600 )edit

As a side note; where can I find the status/plans of Python 3 adoption in ROS?

alf gravatar image alf  ( 2017-07-28 01:31:54 -0600 )edit

Were you able to get ROS running for python3?

zishan gravatar image zishan  ( 2017-12-20 03:46:24 -0600 )edit

I don't believe there is anyone working on it actively, but we are quite close to have full support. See my answer below for details.

v4hn gravatar image v4hn  ( 2018-09-08 04:26:27 -0600 )edit
11

answered 2018-09-08 04:25:26 -0600

v4hn gravatar image

The situation changed slightly over the last two years. You can compile a complete ROS distribution (I tried kinetic) with

catkin build --cmake-args -DPYTHON_VERSION=3.6

assuming you setup a whole core workspace with something like

rosinstall_generator --rosdistro kinetic desktop_full

This works more or less for the core ROS system. I just filed a few patches to make it even work with MoveIt.

However, Dirk's comment is still true. Sadly even in ROS-melodic (which very few people are using already) there is no enforced python3 support. By now, this is simply hilarious. But ROS2 will of course make everything better... :/

I had to fix a few lines of code in a number of packages I use myself (including tf), but they are all straight-forward python2-3 compatibility issues that are well documented.

So everyone, feel free to try it out and file patches if you find more python2-only code somewhere! We are not that far away from Python3 support anymore.

edit flag offensive delete link more

Comments

Can you please provide some links or a detailed description of how you built ROS with Python 3?

rajat7 gravatar image rajat7  ( 2018-09-21 12:22:52 -0600 )edit

@rajat7 this is an old post with an accepted answer. This means that most people will not be looking here to answer questions. If you'd like help please ask your own question. The more complete your question the better answer you can expect. https://wiki.ros.org/Support has some suggestions.

tfoote gravatar image tfoote  ( 2018-09-21 12:56:05 -0600 )edit

This is the best thread for python3 discussions at the moment.

@rajat7: I believe I explained it in quite some detail above and even gave the command lines you need to run. I don't know of any links with more explanations.

v4hn gravatar image v4hn  ( 2018-09-21 17:40:27 -0600 )edit
1

I ran this command, solved each python error as it came and did it all again But I'm stuck at this error import rospkg ImportError: No module named rospkg How to solve this?

rajat7 gravatar image rajat7  ( 2018-09-22 08:48:28 -0600 )edit

Edit: Solved the above error by setting PYTHONPATH. This is the latest one

File "/usr/lib/python3/dist-packages/yaml/__init__.py", line 284
class YAMLObject(metaclass=YAMLObjectMetaclass):
                          ^

SyntaxError: invalid syntax

rajat7 gravatar image rajat7  ( 2018-09-22 08:49:28 -0600 )edit

This syntax is proper according to Python 3. It might be the case that it is still using the default python interpreter instead of python3. Do I need to pass another argument like -DPYTHON_EXECUTABLE=/usr/bin/python3?

rajat7 gravatar image rajat7  ( 2018-09-23 00:46:49 -0600 )edit

Hi sir @v4hn ,

You said: "I just filed a few patches to make it even work with MoveIt."

Could I know how to make it work in detail?

I read your answers in here https://github.com/ros-planning/movei..., and here https://github.com/ros-planning/movei... . But there is no such an answer on how to play Moveit in python 3 with Melodic.

Any help would be highly appreciated!!

All the best,

Bourne gravatar image Bourne  ( 2021-01-16 07:45:43 -0600 )edit
8

answered 2019-08-19 04:09:35 -0600

gvdhoorn gravatar image

updated 2019-08-27 03:15:49 -0600

This will likely become obsolete with the release of Noetic, but as I've just had to build a version of Kinetic with Python 3 support (for the Nth time), I figured I'd post the commands I've used here (for myself as well).

This is a more copy-pastable version of @v4hn's answer (but was too long for a comment).

Assumptions:

  • OS: Ubuntu Xenial (16.04)
  • ROS Kinetic is already installed on that system (the .deb distributed version)

I always use a virtual environment, as it's typically the case that besides ROS, there are Python modules that need to be installed one might not want to "pollute" the system with.

Notes:

  1. this only builds a bare-bones ROS Kinetic with Python 3. No Open CV or other 'complex' packages/nodes. For many use-cases this is already quite sufficient though.
  2. as such, this is not an answer to the general problem of how to build all of ROS 1 with Python 3 support.

In any case, as follows:

# create a place for our Python3-ROS-Kinetic to live
mkdir $HOME/rospy3_kinetic
cd $HOME/rospy3_kinetic

# make sure these are installed
apt install python3-venv libpython3-dev python-catkin-tools

python3.5 -m venv venv3.5_ros
source venv3.5_ros/bin/activate

# these modules are needed but are not automatically installed
pip3 install wheel empy defusedxml netifaces

# just the standard build-ros-from-source dependencies
pip3 install -U rosdep rosinstall_generator wstool rosinstall

# seed the workspace with a 'ros_base' set of packages
rosinstall_generator --deps --tar --rosdistro=kinetic ros_base > ros_base.rosinstall
wstool init src -j8 ros_base.rosinstall

# the following is somewhat optional: I like to run this to see if
# something obvious is missing, but we can't ask rosdep to 'install'
# things for us: it will try to use apt to install the Python 2 version
# of all dependencies it sees missing.

# this may fail if you already have done this, in that case: ignore
sudo rosdep init
rosdep update

# I use check here to first see what is missing. If missing dependencies are
# serious enough, use 'pip3' to install into the virtual environment for
# Python dependencies, 'apt' for any other dependencies
rosdep check --from-paths src/ -i

# finally build the workspace, while:
#  - disabling testing globally
#  - making sure optimisations are enabled ('Release' build type)
#  - forcing use of Python 3.5
catkin build -DCATKIN_ENABLE_TESTING=0 -DCMAKE_BUILD_TYPE=Release -DPYTHON_VERSION=3.5

At this point source the setup.bash of the workspace. You may choose to configure catkin_tools to create an install space instead of using the devel space directly.

For regular usage you'll need to make sure to have the virtual environment activated, or things will break.

I'd recommend using workspace overlaying for actual development.

edit flag offensive delete link more

Comments

@gvdhoorn I tried using the code you have provided to build. It seems that it can build 53 of the packages out of 71 required. It stopped at rosout showing failed errors. It abandoned building other important packages such as roslaunch, rosnode, and rostopic packages. Does this mean I lost all these important functionalities when I rebuild a package in the virtual environment?

haloted gravatar image haloted  ( 2019-09-24 00:49:50 -0600 )edit

As I wrote:

this only builds a bare-bones ROS Kinetic with Python 3. No Open CV or other 'complex' packages/nodes. For many use-cases this is already quite sufficient though. As such, this is not an answer to the general problem of how to build all of ROS 1 with Python 3 support.

It could be that you're trying to build packages that require more patching.

But I'm not sure what exactly you are trying to build, so I cannot say much.

Does this mean I lost all these important functionalities when I rebuild a package in the virtual environment?

I don't know what this means.

gvdhoorn gravatar image gvdhoorn  ( 2019-09-24 02:43:04 -0600 )edit

@gvdhoorn, you probably already know this, but you can use rosdep with Python3 now by setting ROS_PYTHON_VERSION=3. https://github.com/ros-infrastructure.... Hope that helps others who end up at this answer.

Thomas D gravatar image Thomas D  ( 2020-03-14 17:29:34 -0600 )edit
-1

answered 2019-09-30 10:58:13 -0600

Not sure about the previous versions of ROS, but it easy to build ROS packages with python3 in ROS Melodic.

Modify the following file:

/opt/ros/melodic/etc/catkin/profile.d/1.ros_python_version.sh

from

export ROS_PYTHON_VERSION=2

to

export ROS_PYTHON_VERSION=3

Source the above-mentioned file.

source /opt/ros/melodic/etc/catkin/profile.d/1.ros_python_version.sh

Create a new workspace and build.

If you have an existing workspace, delete the devel/, build/ and .catkin_workspace file from your workspace and build again.

edit flag offensive delete link more

Comments

Would you really need to edit system/package managed files for that? If catkin ever updates, your changes will be lost.

Could you not add export ROS_PYTHON_VERSION=3 to your .bashrc?

Also note: this would not seem to influence the Python version used by any of the packages installed using apt. So base ROS packages would still not 'use' Python 3 (whatever that means).

gvdhoorn gravatar image gvdhoorn  ( 2019-09-30 15:03:41 -0600 )edit

Yes, adding to .bashrc is a much better solution. In my case, I manage many workspaces for different use cases.

"Also note: this would not seem to influence the Python version used by any of the packages installed using apt. So base ROS packages would still not 'use' Python 3 (whatever that means)" I feel it hold true for the accepted answer as well in case of apt install. But I have not come across this issue till now.

Ashish K gravatar image Ashish K  ( 2019-09-30 22:25:38 -0600 )edit

adding to .bashrc is a much better solution.

I don't think every ros developers would agree with this statement. Some might.

130s gravatar image 130s  ( 2021-04-23 21:07:34 -0600 )edit

Question Tools

8 followers

Stats

Asked: 2016-06-21 02:17:10 -0600

Seen: 69,419 times

Last updated: Sep 30 '19