Ask Your Question
2

How to run ROS if your default python version is 3.x via Anaconda

asked 2017-04-17 15:31:55 -0600

alexe gravatar image

Hey there,

the default python version I use on my Ubuntu 16.04 machine is 3.6 (Anaconda) and I'm trying to setup my catkin workspace. When I run catkin_make, I get an error that has been described in a few other questions here (such as here and here):

ImportError: "from catkin_pkg.package import parse_package" failed: No module named 'catkin_pkg'
Make sure that you have installed "catkin_pkg", it is up to date and on the PYTHONPATH.

The suggestions in the other questions don't help me, as this is about a more general problem:

How can I run ROS (Kinetic in my case) when my default python version is 3.x?

I want to make it so that ROS uses python 2.7, but I want to keep python 3.6 as my default python version.

Is that possible? If yes, how do I do that?

This is my default python version:

$ python -V
Python 3.6.0 :: Anaconda 4.3.1 (64-bit)

And my sys.path:

$ python -c 'import sys; print(sys.path)'
['', '/opt/ros/kinetic/lib/python2.7/dist-packages', '/home/xyz/anaconda3/lib/python36.zip', '/home/xyz/anaconda3/lib/python3.6', '/home/xyz/anaconda3/lib/python3.6/lib-dynload', '/home/xyz/anaconda3/lib/python3.6/site-packages', '/home/xyz/anaconda3/lib/python3.6/site-packages/Sphinx-1.5.1-py3.6.egg', '/home/xyz/anaconda3/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg']

I tried to simply add /usr/lib/python2.7/ to my ~/.bashrc, but that doesn't work. I can't even source the .bashrc because of syntax errors in all files with python 2 syntax.

My logic was that if I simply add all python directories, version 2.x and 3.x, to my PYTHONPATH, then any application (such as ROS) can always find the right modules it needs. But it doesn't seem to work like that, or at least simply adding python paths to .bashrc. doesn't work.

Weirdly, the path '/opt/ros/kinetic/lib/python2.7/dist-packages' is already in there, but it is /usr/lib/python2.7/dist-packages that contains catkin_pkg and some other important packages and modules.

Any help is very much appreciated!

edit retag flag offensive close merge delete

6 Answers

Sort by ยป oldest newest most voted
0

answered 2017-04-17 17:56:07 -0600

alexe gravatar image

Creating a conda environment with python 2.7 and then PIP installing catking_pkg and running catkin_make from within that environment solves the issue above, but now I'm getting a new error described here.

edit flag offensive delete link more
1

answered 2018-02-15 12:30:04 -0600

130s gravatar image

For Python3 environment, install python3-catkin-pkg.

Also, this thread might be worth reading for using alternative Python interpreters with ROS.

edit flag offensive delete link more
0

answered 2018-02-23 09:56:01 -0600

Mike Zobot gravatar image

updated 2018-02-23 09:56:49 -0600

Here is my approach:

If you want to to keep ROS and anaconda separated only

  1. remove anaconda path in your bashrc file
  2. uninstall your ros
  3. close all terminal and reinstall ros (you probably need to delete the rosdep folder to re init)

you should be able to use ROS now. I would recommend source the ros when you need it or update the anaconda path when you need it.

edit flag offensive delete link more
0

answered 2019-10-10 18:27:37 -0600

After looking at zwkcoding answer, I did a quick search and it works. To complement:

Use in your terminal window: conda deactivate to use ROS and conda activate to use Anaconda.

Only works on conda 4.6 and later versions. For conda versions prior to 4.6, run:

    Windows: **activate** or **deactivate**

    Linux and macOS: **source activate** or **source deactivate**

Do this and you don't have to change anything in .bashrc or any other files.

Sources: An answer from a question somewhere else; and https://conda.io/projects/conda/en/la...

edit flag offensive delete link more
0

answered 2018-08-23 20:29:09 -0600

emilyfy gravatar image

I found the solution on this issue on github. Since the Anaconda path is the one interfering with ROS, you can simply comment out the prepending of Anaconda on the PATH environment variable. In my case I just created a bash script with export PATH=`echo $PATH | sed -e 's/:\/home\/user\/anaconda3\/bin//'` in it and source it every time I want to run ROS.

edit flag offensive delete link more

Comments

doesn't work

zwkcoding gravatar imagezwkcoding ( 2019-05-21 10:55:32 -0600 )edit
0

answered 2019-05-21 10:59:22 -0600

zwkcoding gravatar image

The easiest method is to deactivate anaconda env when you use ROS

$ source deactivate Try it, you'll be appreciated!

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2017-04-17 15:10:43 -0600

Seen: 6,177 times

Last updated: Oct 10