[ubuntu]安装并使用python 3.6及与2.7的切换

Ubuntu下Python3.6安装与配置

当前使用ubuntu14.04

1.添加python3.6安装包,并安装(也可以去官网下载安装包)

linux 报错E: Unable To Locate Package Software-properties-common
https://blog.youkuaiyun.com/qq_27435059/article/details/80508553


 
sudo apt-get install software-properties-common

 

 

sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6


2.用which python查看默认安装python的目录

cd /usr/bin
sudo rm python

 

3.用which python3.6查看安装python3.6的目录


 

4.用python3.6替换python2.7

 

sudo ln -s /usr/local/bin/python3.6 /usr/bin/python


这时在终端中输入python可看到:

 

 

 

FINAL:切换到2.7,同理。

<think> 我们正在处理一个关于在Ubuntu 20.04上配置ROS Melodic以使用Python 3.6的问题。请注意,ROS Melodic原本是为Ubuntu 18.04设计的,默认使用Python 2.7。然而,Ubuntu 20.04默认的Python版本是3.8,这可能导致兼容性问题。用户希望使用Python 3.6。 根据引用[1]和引用[2],我们知道: - 引用[1]提到在Ubuntu18.04安装ROS Melodic的步骤,其中提到了rosdep update失败的问题,推荐了解决方法。 - 引用[2]则提到了在Ubuntu20.04中由于Python版本问题导致ROS不能运行的情况,展示了如何调整Python3的默认版本(在3.63.8之间切换)。 但是,用户的需求是在Ubuntu20.04上让ROS Melodic使用Python3.6。由于Ubuntu20.04自带Python3.8,而ROS Melodic(默认用于Ubuntu18.04)在Ubuntu20.04上安装时,可能会遇到依赖问题。因此,我们需要配置ROS Melodic使用Python3.6。 步骤概述: 1. 安装Ubuntu20.04可用的Python3.62. 设置Python3.6为系统默认的Python3版本(可选,但可能需要,因为ROS Melodic在构建时可能会使用默认的Python3)。 3. 安装ROS Melodic,可能需要从源码构建,指定使用Python3.6。 4. 针对需要的ROS包(如cv_bridge)重新编译以支持Python3.6。 详细步骤: ### 1. 安装Python3.6Ubuntu20.04上,Python3.6不是默认安装的,但可以通过apt安装: ```bash sudo apt update sudo apt install python3.6 ``` ### 2. 设置Python3.6为默认的Python3版本(可选) 使用update-alternatives来管理多个Python3版本: ```bash sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1 sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2 ``` 然后通过以下命令切换: ```bash sudo update-alternatives --config python3 ``` 选择Python3.6对应的编号。 注意:修改系统默认的Python3版本可能会导致系统组件出现问题,因为Ubuntu20.04的一些系统工具依赖于Python3.8。因此,更安全的做法是不改变系统默认的Python3版本,而是仅在编译ROS时指定使用Python3.6。 ### 3. 安装ROS Melodic 由于Ubuntu20.04不是ROS Melodic的官方支持平台,我们需要从源码构建ROS Melodic,在构建时指定使用Python3.63.1 设置sources.list ```bash sudo sh -c &#39;echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list&#39; ``` 3.2 设置密钥 ```bash sudo apt-key adv --keyserver &#39;hkp://keyserver.ubuntu.com:80&#39; --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654 ``` 3.3 安装依赖 ```bash sudo apt update sudo apt install -y python-rosdep python-rosinstall-generator python-wstool python-rosinstall build-essential ``` 3.4 初始化rosdep ```bash sudo rosdep init rosdep update ``` 注意:rosdep update可能会失败,可以参考引用[1]中提到的解决方法。 3.5 创建工作空间下载ROS Melodic源码 ```bash mkdir -p ~/ros_catkin_ws cd ~/ros_catkin_ws rosinstall_generator desktop_full --rosdistro melodic --deps --tar > melodic-desktop-full.rosinstall wstool init -j8 src melodic-desktop-full.rosinstall ``` 3.6 安装依赖 ```bash rosdep install --from-paths src --ignore-src --rosdistro melodic -y ``` ### 4. 指定使用Python3.6构建 在构建之前,需要修改catkin的配置,使其使用Python3.6。 4.1 安装Python3.6的依赖 确保为Python3.6安装必要的ROS依赖包,如rospkg等: ```bash pip3.6 install -U pip pip3.6 install -U setuptools pip3.6 install catkin_pkg rospkg numpy ``` 4.2 修改构建配置 在catkin的配置文件中指定Python版本。在`~/ros_catkin_ws`目录下,创建或修改`catkin_ws/config.yaml`文件(如果没有,可以在调用`catkin_make`时通过参数指定): ```yaml # config.yaml python: python-version: 3 executable: /usr/bin/python3.6 numpy: /usr/lib/python3/dist-packages/numpy ``` 4.3 构建ROS ```bash cd ~/ros_catkin_ws ./src/catkin/bin/catkin_make_isolated --install -DCMAKE_BUILD_TYPE=Release -DPYTHON_EXECUTABLE=/usr/bin/python3.6 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so ``` 注意:上面的路径可能需要根据实际安装情况调整。 ### 5. 环境设置 构建完成后,设置环境变量: ```bash echo "source ~/ros_catkin_ws/install_isolated/setup.bash" >> ~/.bashrc source ~/.bashrc ``` ### 6. 测试 启动roscore,测试是否正常运行: ```bash roscore ``` ### 注意事项 - 由于在非官方支持的系统版本上安装,可能会遇到各种依赖问题,需要根据错误信息逐个解决。 - 对于一些特定的包(如cv_bridge),可能需要单独重新编译以支持Python3.6,方法上面类似,指定Python3.6的路径。 - 如果不希望影响系统默认的Python3版本,可以在虚拟环境(如virtualenv或conda)中安装Python3.6,然后在虚拟环境中构建ROS。但这样可能会增加复杂性。 ### 替代方案 考虑使用Docker来运行ROS Melodic,这样可以在容器中使用Ubuntu18.04,从而避免系统Python版本冲突的问题。Docker镜像中已经配置好ROS Melodic,可以直接使用Python2.7Python3.6(如果镜像中安装Python3.6)。 例如,使用官方的ROS Melodic镜像: ```bash docker pull ros:melodic docker run -it ros:melodic ``` 然后在容器中单独安装Python3.6(如果需要)配置ROS包。 ### 相关问题 1.Ubuntu20.04上安装ROS Melodic时遇到依赖问题如何解决? 2. 如何为特定的ROS包(如cv_bridge)编译Python3.6支持? 3. 使用Docker运行ROS Melodic有什么优缺点? 请根据实际情况调整步骤,如果遇到问题,可以查阅ROS社区的相关讨论。 </think> ### 在 Ubuntu 20.04 中配置 ROS Melodic 使用 Python 3.6 的详细方法 虽然 ROS Melodic 官方仅支持 Ubuntu 18.04,但通过以下步骤可在 Ubuntu 20.04 上强制使用 Python 3.6: #### 步骤 1:安装 Python 3.6 和必要组件 ```bash # 添加 Python 3.6 源 sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update # 安装 Python 3.6 核心 sudo apt install python3.6 python3.6-dev # 安装虚拟环境工具 sudo apt install python3.6-venv ``` #### 步骤 2:创建 Python 3.6 虚拟环境 ```bash # 创建专用环境目录 mkdir -p ~/ros_py36 && cd ~/ros_py36 # 创建虚拟环境 python3.6 -m venv melodic_py36 source melodic_py36/bin/activate # 确认环境 python --version # 应显示 Python 3.6.x ``` #### 步骤 3:在虚拟环境中安装 ROS Melodic ```bash # 设置ROS源 sudo sh -c &#39;echo "deb http://packages.ros.org/ros/ubuntu bionic main" > /etc/apt/sources.list.d/ros-latest.list&#39; sudo apt-key adv --keyserver &#39;hkp://keyserver.ubuntu.com:80&#39; --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654 # 安装依赖(在虚拟环境中) pip install rosinstall-generator rosdep wstool catkin-pkg # 初始化ROS sudo rosdep init rosdep update ``` #### 步骤 4:编译核心组件(关键步骤) ```bash # 创建工作空间 mkdir -p ~/melodic_ws/src && cd ~/melodic_ws # 生成构建文件 rosinstall_generator ros_comm --rosdistro melodic --deps --tar > melodic-ros_comm.rosinstall wstool init src melodic-ros_comm.rosinstall # 解决依赖 rosdep install --from-paths src --ignore-src -y --rosdistro melodic # 强制使用 Python 3.6 编译 catkin_make -DPYTHON_EXECUTABLE=$(which python) \ -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m \ -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so ``` #### 步骤 5:环境配置测试 ```bash # 添加到环境变量 echo "source ~/melodic_ws/devel/setup.bash" >> ~/.bashrc source ~/.bashrc # 测试 Python 绑定 python -c "import rospy; print(&#39;Success! ROS version:&#39;, rospy.__version__)" ``` #### 注意事项: 1. **关键依赖问题** 可能需要手动修复依赖关系: ```bash # 修复常见的 cv_bridge 问题 sudo apt install ros-melodic-cv-bridge -y cd ~/melodic_ws/src git clone https://github.com/ros-perception/vision_opencv.git catkin_make --only-pkg-with-deps cv_bridge ``` 2. **版本冲突处理** 使用 `update-alternatives` 管理系统默认版本[^2]: ```bash sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1 sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2 sudo update-alternatives --config python3 # 选择3.6 ``` 3. **替代方案推荐** 对于长期项目,建议: - 使用 Docker 容器封装 Ubuntu 18.04 + Python 3.6 环境 ```bash docker pull ros:melodic docker run -it --rm ros:melodic ``` - 或升级到 ROS Noetic(原生支持 Python 3) > **重要提示**:此配置可能导致 `roscore` 等核心组件异常,建议仅用于特定节点的 Python 3.6 兼容需求。完整系统迁移需重新编译所有依赖包[^1]。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值