First time here? Check out the FAQ!


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 Jun 21 '16

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

Preview: (hide)

Comments

1

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

Tanmay gravatar image Tanmay  ( Oct 3 '16 )edit
1

Were you able to get ROS running for python3?

zishan gravatar image zishan  ( Dec 20 '17 )edit

4 Answers

Sort by » oldest newest most voted
16

answered Jun 21 '16

Dirk Thomas gravatar image

updated Aug 27 '19

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

Preview: (hide)

Comments

thank you.

SHPOWER gravatar image SHPOWER  ( Jun 22 '16 )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  ( Jun 22 '16 )edit

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

alf gravatar image alf  ( Jul 28 '17 )edit

Were you able to get ROS running for python3?

zishan gravatar image zishan  ( Dec 20 '17 )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  ( Sep 8 '18 )edit
11

answered Sep 8 '18

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.

Preview: (hide)

Comments

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

rajat7 gravatar image rajat7  ( Sep 21 '18 )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  ( Sep 21 '18 )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  ( Sep 21 '18 )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  ( Sep 22 '18 )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  ( Sep 22 '18 )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  ( Sep 23 '18 )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  ( Jan 16 '21 )edit
8

answered Aug 19 '19

gvdhoorn gravatar image

updated Aug 27 '19

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.

Preview: (hide)

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  ( Sep 24 '19 )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  ( Sep 24 '19 )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  ( Mar 14 '20 )edit
-1

answered Sep 30 '19

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.

Preview: (hide)

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  ( Sep 30 '19 )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  ( Oct 1 '19 )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  ( Apr 24 '21 )edit

Question Tools

8 followers

Stats

Asked: Jun 21 '16

Seen: 69,433 times

Last updated: Sep 30 '19