• <Android开发> 开发工具python- 之-pip安装使用说明


    <Android开发> 开发工具python- 之-pip安装使用说明

    前言:目前对于ubuntu 20.04系统都会自带python,基于实际开发工作要求,有时需要使用python2 ,有时又需要使用python3;而它们两涉及的库有各自调用。例如数据库 xlrd,如果是使用python3安装的,则再python2中无法使用;
    基于该问题讲解一下,python2和python3安装以及使用问题;

    1、安装python
    python2安装命令(2.7):

    sudo apt-get install python2
    
    • 1

    2、查看已安装python
    命令:

    find /usr/bin/ -name python*
    ls /usr/local/lib
    
    • 1
    • 2

    在这里插入图片描述
    3、配置系统默认python版本
    (1)查看python代替版本信息,如果提示“error”,表示当前系统中python代替版本尚未安装;
    命令:

    sudo update-alternatives --list python
    
    • 1

    (2)配置python
    将/usr/bin/python2.7优先级设置为1,命令:

    sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
    
    • 1

    将/usr/bin/python3.8的优先级设置为2,命令:

    sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2
    
    • 1

    由于python3的优先级大于python2的优先级,此时系统默认的python版本是python3

    (3)重新查看python代替版本信息,命令:

    sudo update-alternatives --list python
    
    • 1

    在这里插入图片描述

    4、查看python 默认版本
    命令:

    python -V 
    或
    python --version
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    5、切换python版本
    命令:

    sudo update-alternatives --config python
    
    • 1

    在这里插入图片描述
    根据上图,输入对应的编号,即可设置系统默认的python版本;如输入“1”,回车,此时系统默认python将变为python2,入下图所示:
    在这里插入图片描述
    至此,python2和python3同时安装完成;

    6、安装pip
    (1)python3安装pip,命令:

    sudo apt install python3-pip
    
    • 1

    (2)python2安装pip,命令:

    sudo apt install python-pip
    
    • 1

    python2安装pip的命令运行后会提示找不到软件包,如下图:
    在这里插入图片描述
    此时需要手动下载安装;

    首先安装curl工具,命令:

    sudo apt-get install curl
    
    • 1

    接着下载python2.7的pip脚本,命令:

    sudo curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py 
    或
    sudo curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py 
    
    • 1
    • 2
    • 3

    下载脚本可能会断,或失败;可打开对应网页下载;
    在这里插入图片描述
    最后运行脚本,安装python2的pip,命令:

    sudo python get-pip.py 
    
    • 1

    在这里插入图片描述

    7、以安装xlrd为例
    python3下安装xlrd,命令:

    sudo update-alternatives --config python  #切换python版本为python3
    python --version   #查看当前默认python版本
    pip --version		#查看当前pip的默认python版本
    pip install xlrd==1.2.0	#安装xlrd库
    
    也可使用
    pip install xlrd
    但是可能会由于安装的版本过高,导致无法使用。所以建议安装 1.2.0版本即可;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    python2下安装xlrd,命令:

    sudo update-alternatives --config python  #切换python版本为python2
    python --version   #查看当前默认python版本
    pip --version		#查看当前pip的默认python版本
    pip install xlrd==1.2.0	#安装xlrd库
    
    也可使用
    pip install xlrd
    但是可能会由于安装的版本过高,导致无法使用。所以建议安装 1.2.0版本即可;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    总结,至此python2和python3交替使用,和对应的库安装讲解完毕,如有不足、遗漏之处,还请读者给予留言反馈,谢谢。

  • 相关阅读:
    通过QGIS下载高程数据并进行矢量数据高程的计算
    笔记:电子设备接地,接的到底是什么地?
    【C++深入浅出】类和对象下篇
    HTML大学班级活动网页设计 、大学校园HTML实例网页代码 、本实例适合于初学HTML的同学
    Jammy@Jetson Orin - Tensorflow & Keras Get Started: 001 Linear Regression
    Windows环境VSCode配置OpenCV-项目配置(二)
    GBPC2510W-ASEMI马达专用方桥GBPC2510W
    Word隐藏批注知识分享,快速提升工作效率!
    Python easyOCR图像文本提取 初识
    这个IfxCpu_Trap_busError函数是干嘛的
  • 原文地址:https://blog.csdn.net/qq_39257814/article/details/125408545