机器人的工控机在安装了一些python package后, 发现一运行roscore 电脑就重启. 奇怪得很. 也不弹报错,直接重启. 因为机器人上的工控机很多人在使用, 每个人安装的东西都一样, 需要的环境要求也不一样, 特别是安装python的package,基本都是使用python3, 跟ROS系统所使用的python2.7有冲突, 这也是导致roscore启动失败的原因.
一开始也找不到方向, 各种搜索也找不到对应的解决方法, 后来实在没办法了, 只能重装ROS, 然后就发现问题所在了. 当运行安装ROS最后两句命令:
sudo rosdep init
rosdep update
会报错,如下:
user@user-desktop:~$ sudo rosdep init Traceback (most recent call last): File "/usr/bin/rosdep", line 6, infrom pkg_resources import load_entry_point File "/home/user/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3238, in @_call_aside File "/home/user/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3222, in _call_aside f(*args, **kwargs) File "/home/user/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3251, in _initialize_master_working_set working_set = WorkingSet._build_master() File "/home/user/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 567, in _build_master ws.require(__requires__) File "/home/user/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 884, in require needed = self.resolve(parse_requirements(requirements)) File "/home/user/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 770, in resolve raise DistributionNotFound(req, requirers) pkg_resources.DistributionNotFound: The 'rosdep==0.21.0' distribution was not found and is required by the application
意思就是找不到对应的版本,但是rosdep是已经下载成功了的, 还是说找不到, 问题就出在这里, 我们已经安装的是Python2的rosdep, 但现在系统默认的python版本是python3, 所以我们没有安装python3的rosdep, 当然,有人说到这里可以装python3的rosdep, 但是后果是会把ros很多package删掉, 我没试过,但是根据现在的ROS版本, 目前基于python3的ROS肯定有很多package没有的.
所以改回python2才是正途.
我试了直接在.bashrc里设置默认的python版本为2.7,
alias python='/usr/bin/python2.7'
,虽然用python -V显示 当前python版本是2.7, 但是在init rosdep的时候还是失败, 说明编译的默认python版本没有换过来
用另一种方法设置默认python版本:
- 检查当前的python版本:
ls /usr/bin/python* - 设置当前的python版本
sudo update-alternatives --config python
会显示如下信息:
user@user-desktop:~$ sudo update-alternatives --config python There are 3 choices for the alternative python (providing /usr/bin/python). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/python3.6 2 auto mode 1 /usr/bin/python2.7 1 manual mode 2 /usr/bin/python3 1 manual mode 3 /usr/bin/python3.6 2 manual mode Pressto keep the current choice[*], or type selection number: 1 update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in manual mode
可以看到星号所在的是第一个选项,python3.6, 所以只要输入1, 它对应的是python2.7就能解决所有问题了.
- 输入完,再次检查当前版本
sudo update-alternatives --config python
user@user-desktop:~$ sudo update-alternatives --config python There are 3 choices for the alternative python (providing /usr/bin/python). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/bin/python3.6 2 auto mode * 1 /usr/bin/python2.7 1 manual mode 2 /usr/bin/python3 1 manual mode 3 /usr/bin/python3.6 2 manual mode Pressto keep the current choice[*], or type selection number:
现在的python版本是2.7, 问题解决!
- 这时再run sudo rosdep ini
出现如下error, 没问题,只是提示我们已经设置了初始化的文件,这一步 只是想测试rosdep init能不能运行.
ERROR: default sources list file already exists: /etc/ros/rosdep/sources.list.d/20-default.list Please delete if you wish to re-initialize
这也是因为我之前已经装好了ROS才会出这个error, 如果第一次安装是不会出现这个的.
- 运行 rosdep update
到这里就结束了,此时再run roscore就不会电脑重启了
- wiki.ros.org
- https://blog.csdn.net/moX980/article/details/110316752



