Modify environment variable so that solving Anaconda与ROS共存问题

when i tried to add a publisher of ROS to a tensorflow python file, i got follow error:

ImportError: No module named rospkg

and i found a follow solution Link

问题描述

在安装Anaconda后

$ cd ~/catkin_ws
$ catkin_make

编译报错

ImportError: No module named rospkg

问题分析

刚开始以为rospkg出了差错,使用以下命令查错。

$ sudo apt-get install python-rospkg

发现rospkg包已经更新到最新版本,那么就基本排除ROS本身Python包出错的可能性。

又想到,安装Anaconda时有

Do you wish the installer to prepend the Anaconda2 install location to PATH in your /home/tingting/.bashrc ? [yes|no]

此处应该是在环境变量中加了什么奇怪的东西影响到ROS的环境变量。为了探究明白,查看环境变量。

$ echo $PATH

结果如下

/home/hanxy/anaconda2/bin:/opt/ros/indigo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

查看~/.bashrc文件

$ sudo gedit ~/.bashrc

发现文件末尾有Anaconda添加的$PATH变量

$ export PATH=/home/hanxy/anaconda2/bin:$PATH

解决方法

初步解决

想要快速解决很简单,注释掉上述导入环境变量的语句,但同时Anaconda内的包不能正常使用,因此每次开启终端都应该输入上述语句,以获得Anaconda的环境变量。
但这样在Anaconda中仍然无法使用ROS包,在ROS中也无法使用Anaconda的包,这将是很大的损失。

彻底解决

无法在Anaconda中导入ROS包归根结底是由于没有相应的包,因此

$ conda install setuptools
$ pip install -U rosdep rosinstall_generator wstool rosinstall six vcstools
$ hanxy@hanxy-Ubuntu:~$ python
Python 2.7.14 |Anaconda, Inc.| (default, Oct 16 2017, 17:29:19) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
>>> import rospkg
>>> 

同时导入tensorflow和rospkg成功~

While it did not do much contribution, the publisher worked, but when i roslaunch a moveit related package, the similiar error occured again, it seem you need install other packages in conda environment, so it must not be the right solution.

Actually, you just need modify the .bashrc file as follow content.

export PATH="$PATH:/home/robot/anaconda3/bin"

While it seems these two parts worked perfectly!

原文地址:https://www.cnblogs.com/siahekai/p/11000786.html