0
$\begingroup$

Rosanswers logo

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


Originally posted by SHPOWER on ROS Answers with karma: 302 on 2016-06-21

Post score: 17


Original comments

Comment by Tanmay on 2016-10-03:
Hello, did you happen to find any way to get ROS to work with Python3?

Comment by zishan on 2017-12-20:
Were you able to get ROS running for python3?

$\endgroup$

3 Answers 3

0
$\begingroup$

Rosanswers logo

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


Originally posted by Dirk Thomas with karma: 16276 on 2016-06-21

This answer was ACCEPTED on the original site

Post score: 16


Original comments

Comment by SHPOWER on 2016-06-22:
thank you.

Comment by Dirk Thomas on 2016-06-22:
Please mark the answer as accepted if it answered your question so that others find it now easily.

Comment by alf on 2017-07-28:
As a side note; where can I find the status/plans of Python 3 adoption in ROS?

Comment by zishan on 2017-12-20:
Were you able to get ROS running for python3?

Comment by v4hn on 2018-09-08:
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.

$\endgroup$
0
$\begingroup$

Rosanswers logo

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.


Originally posted by v4hn with karma: 2950 on 2018-09-08

This answer was NOT ACCEPTED on the original site

Post score: 11


Original comments

Comment by rajat7 on 2018-09-21:
Can you please provide some links or a detailed description of how you built ROS with Python 3?

Comment by tfoote on 2018-09-21:
@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.

Comment by v4hn on 2018-09-21:
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.

Comment by rajat7 on 2018-09-22:
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?

Comment by rajat7 on 2018-09-22:
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

Comment by rajat7 on 2018-09-23:
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?

Comment by Bourne on 2021-01-16:
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/moveit/issues/1722, and here https://github.com/ros-planning/moveit/issues/1743 . 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,

$\endgroup$
0
$\begingroup$

Rosanswers logo

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.


Originally posted by gvdhoorn with karma: 86574 on 2019-08-19

This answer was NOT ACCEPTED on the original site

Post score: 8


Original comments

Comment by haloted on 2019-09-24:
@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?

Comment by gvdhoorn on 2019-09-24:
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.

Comment by Thomas D on 2020-03-14:
@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/rosdep/pull/709. Hope that helps others who end up at this answer.

$\endgroup$

Your Answer

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