0


ROS修改:ubuntu系统更改默认python版本(重要操作)

ubuntu18.04+ROS melodic

困扰:ROS用到python2,而各种目标检测源码用到python3,而且各种包也要安在python3的环境下,而不是python2.

解决:基于update-alternatives

1.怎么出现的问题

在安装pytorch时发现安装适用于python3.6以上的pytorch版本时出现找不到对应的pytorch版本,而且提示有老的版本,说明在python版本中pip install默认用了python2.*没用python3.*。

rosmelodic@rosmelodic-virtual-machine:~$ pip install torch==1.8.0+cpu torchvision==0.9.0+cpu torchaudio==0.8.0 -f https://download.pytorch.org/whl/torch_stable.html
Collecting torch==1.8.0+cpu
  Could not find a version that satisfies the requirement torch==1.8.0+cpu (from versions: 0.1.6.post17, 0.1.6.post20, 0.1.6.post22, 0.1.7.post2, 0.1.8.post1, 0.1.9.post1, 0.1.9.post2, 0.1.10.post1, 0.1.10.post2, 0.1.11.post4, 0.1.11.post5, 0.1.12.post1, 0.1.12.post2, 0.2.0.post1, 0.2.0.post2, 0.2.0.post3, 0.3.0, 0.3.0.post2, 0.3.0.post3, 0.3.0.post4, 0.3.1, 0.4.0, 0.4.1, 1.0.0, 1.0.1, 1.0.1.post2, 1.1.0, 1.2.0, 1.2.0+cpu, 1.2.0+cu92, 1.3.0, 1.3.0+cpu, 1.3.0+cu100, 1.3.0+cu92, 1.3.1, 1.3.1+cpu, 1.3.1+cu100, 1.3.1+cu92, 1.4.0, 1.4.0+cpu, 1.4.0+cu100, 1.4.0+cu92, 1.5.0+cpu, 1.5.0+cu92)
No matching distribution found for torch==1.8.0+cpu

ubuntu18.04,python版本

rosmelodic@rosmelodic-virtual-machine:~$ python
Python 2.7.17 (default, Jul  1 2022, 15:56:32) 
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
rosmelodic@rosmelodic-virtual-machine:~$ python3
Python 3.6.9 (default, Jun 29 2022, 11:45:57) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

ubuntu20.04,python版本

rosnoetic@rosnoetic-virtual-machine:~/桌面$ python

找不到命令“python”,您的意思是:

  command 'python3' from deb python3
  command 'python' from deb python-is-python3
rosnoetic@rosnoetic-virtual-machine:~/桌面$ python3
Python 3.8.10 (default, Jun 22 2022, 20:18:18) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

ubuntu18.04有两种python版本且默认为2.7.17和3.6.9。

ubuntu20.04有仅支持python3,版本为3.8.10。

由于网上大部分源码已经弃用了python2,故想将ubuntu18.04的默认python版本改为python3,将各种包安装在python3下.

2.修改默认python版本(基于update-alternatives

ls /usr/bin/python*
rosmelodic@rosmelodic-virtual-machine:~$ ls /usr/bin/python*
/usr/bin/python            /usr/bin/python3.6-config
/usr/bin/python2           /usr/bin/python3.6m
/usr/bin/python2.7         /usr/bin/python3.6m-config
/usr/bin/python2.7-config  /usr/bin/python3-config
/usr/bin/python2-config    /usr/bin/python3m
/usr/bin/python2-qr        /usr/bin/python3m-config
/usr/bin/python3           /usr/bin/python-config
/usr/bin/python3.6
python2
rosmelodic@rosmelodic-virtual-machine:~$ python2
Python 2.7.17 (default, Jul  1 2022, 15:56:32) 
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
KeyboardInterrupt
>>> exit()
python3.6
rosmelodic@rosmelodic-virtual-machine:~$ python3.6
Python 3.6.9 (default, Jun 29 2022, 11:45:57) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 2
rosmelodic@rosmelodic-virtual-machine:~$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 1
[sudo] rosmelodic 的密码: 
update-alternatives: 使用 /usr/bin/python3.6 来在自动模式中提供 /usr/bin/python (python)
rosmelodic@rosmelodic-virtual-machine:~$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 2
update-alternatives: 使用 /usr/bin/python2 来在自动模式中提供 /usr/bin/python (python)
sudo update-alternatives --config python
rosmelodic@rosmelodic-virtual-machine:~$ sudo update-alternatives --config python
有 2 个候选项可用于替换 python (提供 /usr/bin/python)。

  选择       路径              优先级  状态
------------------------------------------------------------
* 0            /usr/bin/python2     2         自动模式
  1            /usr/bin/python2     2         手动模式
  2            /usr/bin/python3.6   1         手动模式

要维持当前值[*]请按<回车键>,或者键入选择的编号:2
update-alternatives: 使用 /usr/bin/python3.6 来在手动模式中提供 /usr/bin/python (python)
rosmelodic@rosmelodic-virtual-machine:~$ python
Python 3.6.9 (default, Jun 29 2022, 11:45:57) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

修改默认版本成功。

sudo apt install python3-pip
pip install torch==1.8.0+cpu torchvision==0.9.0+cpu torchaudio==0.8.0 -f https://download.pytorch.org/whl/torch_stable.html

在安装,成功。

在ROS-melodic中用的是python2,故在安装完各种包时,要把默认的python版本调回来,就是把优先级调一下。不然会报错:

rosmelodic@rosmelodic-virtual-machine:~/catkin_ws/src/yolov5-6.0$ roscore
Traceback (most recent call last):
  File "/opt/ros/melodic/bin/roscore", line 36, in <module>
    from rosmaster.master_api import NUM_WORKERS
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rosmaster/__init__.py", line 35, in <module>
    from .main import rosmaster_main
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rosmaster/main.py", line 43, in <module>
    import rosmaster.master
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rosmaster/master.py", line 47, in <module>
    import rosmaster.master_api
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rosmaster/master_api.py", line 72, in <module>
    from rosmaster.util import xmlrpcapi
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rosmaster/util.py", line 48, in <module>
    from defusedxml.xmlrpc import monkey_patch
ModuleNotFoundError: No module named 'defusedxml'

网上各种删除或者降级python版本,属于杀鸡取卵的方法,如果这么做,刚刚下载的各种pyhon依赖的包因为python版本被破坏而失效,调用这些包的代码也会报错。

灵活的调回来,当需要往python3安装新的包时,再调过去。

sudo update-alternatives --config python
rosmelodic@rosmelodic-virtual-machine:~/catkin_ws/src/yolov5-6.0$ python
Python 2.7.17 (default, Jul  1 2022, 15:56:32) 
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
rosmelodic@rosmelodic-virtual-machine:~/catkin_ws/src/yolov5-6.0$ roscore
... logging to /home/rosmelodic/.ros/log/a70de31c-4607-11ed-89a7-000c2969e545/roslaunch-rosmelodic-virtual-machine-19473.log
Checking log directory for disk usage. This may take a while.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://rosmelodic-virtual-machine:46389/
ros_comm version 1.14.13

SUMMARY
========

PARAMETERS
 * /rosdistro: melodic
 * /rosversion: 1.14.13

NODES

auto-starting new master
process[master]: started with pid [19483]
ROS_MASTER_URI=http://rosmelodic-virtual-machine:11311/

setting /run_id to a70de31c-4607-11ed-89a7-000c2969e545
process[rosout-1]: started with pid [19494]
started core service [/rosout]

roscore没问题。

如果要用到刚刚python3搭建的环境,运行的python文件头要加上:

#!/usr/bin/python3

例子:

#!/usr/bin/python3

import rospy
from std_msgs.msg import Int8

import argparse
import os
import sys
from pathlib import Path
标签: ros

本文转载自: https://blog.csdn.net/wzfafabga/article/details/127191057
版权归原作者 机械专业的计算机小白 所有, 如有侵权,请联系我们删除。

“ROS修改:ubuntu系统更改默认python版本(重要操作)”的评论:

还没有评论