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+cpuCould 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 python3command '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_WORKERSFile "/opt/ros/melodic/lib/python2.7/dist-packages/rosmaster/__init__.py", line 35, in <module>from .main import rosmaster_mainFile "/opt/ros/melodic/lib/python2.7/dist-packages/rosmaster/main.py", line 43, in <module>import rosmaster.masterFile "/opt/ros/melodic/lib/python2.7/dist-packages/rosmaster/master.py", line 47, in <module>import rosmaster.master_apiFile "/opt/ros/melodic/lib/python2.7/dist-packages/rosmaster/master_api.py", line 72, in <module>from rosmaster.util import xmlrpcapiFile "/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.13SUMMARY
========PARAMETERS* /rosdistro: melodic* /rosversion: 1.14.13NODESauto-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/python3import rospy
from std_msgs.msg import Int8import argparse
import os
import sys
from pathlib import Path

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

  1. ubuntu 刚更改默认python3版本后更新包等

    ubuntu 刚更改默认python3版本后更新包等 一般来说ubuntu 刚更改为python3为默认版本后要进行一下更新包等等的内容(当然不更新一下也是可以的,最好更新一下,第一次更新较慢) 使用 ...

  2. ubuntu更改默认python版本_更改Ubuntu默认python版本的方法

    1.查看基本信息 # 列出所有已安装python ls /usr/bin/python* #查看默认的 Python 版本信息: python --version 2.基于用户修改 默认Python ...

  3. mac修改默认python版本_Anaconda/MacOS:更改默认python版本

    好吧,我在连续体邮件列表中发现了类似的问题. 通过conda重新安装python解决了这个问题$ conda install python=2.7 Fetching package metadata: ...

  4. CentOS 6升级默认python版本

    CentOS 6作为比较稳定的Linux服务器版本,已经在很多企业的生产环境中运行多年了.然而CentOS 6中原装的python版本仍然为2.6,在开发过程中通常使用python2.7来进行开发.p ...

  5. mac修改默认python版本_Mac系统修改Python版本软链接

    通常Mac系统自带Python版本都是2.x,但在工作中,越来越多的库,框架都需要更高的版本3.x,有三种升级方式,一种是Brew安装,一种是在Python官网下载,还有一种是使用第三方软件,如Ana ...

  6. Ubuntu 修改默认 Python版本

    目录 1 查看Python版本 2 用户级修改 3 系统级修改 3.1 基于软链接 3.2 基于update-alternatives 4 pip错误 1 查看Python版本 先查看系统中有那些Py ...

  7. ubuntu查看python版本-切换Ubuntu默认python版本的两种方法

    你可以按照以下方法使用 ls 命令来查看你的系统中都有那些 Python 的二进制文件可供使用. 1 2 $ls /usr/bin/python* /usr/bin/python /usr/bin/p ...

  8. 关于Ubuntu的默认python版本

    大部分Ubuntu系统默认python版本都是python2.x系列,但最新版本已经是3.5和3.6了,软件系统跟着版本走总是有诸多好处的,所以,以下是作者在修改Ubantu默认python版本时的一 ...

  9. linux修改默认python版本_将Linux下python默认版本切换成替代版本

    当你安装 Debian Linux 时,安装过程有可能同时为你提供多个可用的 Python 版本,因此系统中会存在多个 Python 的可执行二进制文件.你可以按照以下方法使用 ls 命令来查看你的系 ...

最新文章

  1. [Conclusion]RabbitMQ-客户端源码之总结
  2. 80070583类不存在_原创 | 类应该是匀称和均匀的
  3. php 5.6 mcrypt,php 5.6.36 安装mcrypt
  4. 如何在Ubuntu下面识别Galaxy Nexus设备
  5. 代码重用_WebAssembly的速度和代码重用
  6. 微信开发,调用js-SDK接口
  7. bochs运行xp_bochs xp镜像下载
  8. Android实现抖音无水印视频
  9. 0到1使用spring-security(Spring的安全认证框架)
  10. css改变水平线的颜色
  11. Unity调用安卓接口——实现复制粘贴功能
  12. 移动互联网的未来在非洲
  13. Matlab App Designer编译打包exe后读取文件路径问题
  14. 2019年的软件百强企业榜单
  15. Navicat导入xlsx文件提示无法打开文件
  16. BeanDefinitionParsingException Configuration problem Unabl
  17. 用html写京东网页,实现部分功能
  18. 【封装】Deviation求方差
  19. 请在mysql配置文件修sql_mode为NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTIO
  20. linux培训_南通linux培训多少钱

热门文章

  1. JSON 数据转换为 JavaScript 对象
  2. linux命令查看时间属于哪个地区,详解Linux下用date命令查看和计算包含时区的时间戳...
  3. java获取系统date_Java中获取当前时间
  4. 海绵学习法:怎么找到你的10000小时?
  5. 基于Bert+对抗训练的文本分类实现
  6. Java 世界的法外狂徒:反射
  7. 【仙女踩坑实录】解决18MacbookPro外接显示器后发热严重
  8. 用Edge将GitHub上的markdown文件在线完美转pdf
  9. JavaScript前端经典面试题之ES6面试题汇总es6
  10. STL源码:分配器 allocator