• 如何在ubuntu下安装任意版本python


    一、现有的几种安装方式优缺点分析

    最近在编译一个工程的时候遇到问题,看到报错信息指向的是一个py文件,第一反应就是python版本问题,网上一搜果然是需要把版本从3.5升级到3.7。

    网上关于安装python的文章有很多,基本方法分为两种

    1. 方法一添加新的源

    添加新的源,然后用标准的apt install的方式安装
    这种方法的问题是因为年代久远,很多文章里面提供的源已经失效了。而且也只能安装特定的几种版本,做不到任意版本选择。

    参考的链接如下
    https://blog.csdn.net/u013578795/article/details/105460386
    https://blog.csdn.net/qq_40965177/article/details/83500817

    2. 方法二下载源码编译

    下载源码编译,然后删除原来的python,把新的python链接上去。
    这个方法的缺点是不同的项目需要不同版本的python,删掉旧版本会导致别的项目出问题。

    参考的链接如下
    https://blog.csdn.net/qq_44696031/article/details/124564768
    https://blog.csdn.net/MenciusHometown/article/details/77688728

    二、解决方式

    还是采用源码方式编译安装新的python,然后利用update-alternatives来切换自己需要的版本

    1. 源码方式编译python

    1.1 安装编译需要的包

    sudo apt -y install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev libgl1-mesa-dev libxml2-utils xsltproc unzip dosfstools e2fsprogs parted htop tree openssl  perl tofrodos iproute2 gawk  git  xvfb net-tools tftpd flex bison libselinux1 screen pax gnupg diffstat chrpath socat xterm autoconf libtool texinfo gcc-multilib build-essential zlib1g-dev libsdl1.2-dev libglib2.0-dev zlib1g-dev zlib1g-dev:i386 libssl-dev libncurses5-dev    make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev
    
    • 1

    sudo apt-get install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev

    1.2 下载python源代码

    打开python官网 https://www.python.org/ftp/python/
    找到自己需要的版本
    在这里插入图片描述
    这里以python-3.7.1rc2为例

    下载源码,并解压。注意文件名要区分大小写

    wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1rc2.tgz
    tar xvf Python-3.7.1rc2.tgz
    
    • 1
    • 2

    1.3 配置并编译

    配置安装路径。注意这里指定的/usr/local/python3.7,后面会用到这个路径

    cd Python-3.7.1rc2
    ./configure --prefix=/usr/local/python3.7 --enable-optimizations --with-ensurepip=install
    make
    
    • 1
    • 2
    • 3

    1.4 安装

    sudo make install
    
    • 1

    2. 用update-alternatives切换python版本

    2.1 列出所有可用的版本

    2.1.1 update-alternatives --list python

    leo@xlx:~$ update-alternatives --list python 
    update-alternatives: error: no alternatives for python
    
    • 1
    • 2

    2.1.2 解决no alternatives for python错误

    第一次运行上面的命令可能会报错update-alternatives: error: no alternatives for python
    这个错误信息表示 python 的替代版本还没被update-alternatives识别

    要解决这个问题,我们先找到所有的python版本

    leo@xlx:~$ ll /usr/bin/python*
    lrwxrwxrwx 1 root root       9 83 15:11 /usr/bin/python -> python2.7*
    lrwxrwxrwx 1 root root       9 83 15:11 /usr/bin/python2 -> python2.7*
    -rwxr-xr-x 1 root root 3488528 722  2020 /usr/bin/python2.7*
    lrwxrwxrwx 1 root root       9 83 15:11 /usr/bin/python3 -> python3.5*
    -rwxr-xr-x 1 root root 4456208 720  2020 /usr/bin/python3.5*
    -rwxr-xr-x 1 root root 4456208 720  2020 /usr/bin/python3.5m*
    lrwxrwxrwx 1 root root      10 83 15:11 /usr/bin/python3m -> python3.5m*
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    如上图所示,现有的两个版本分别为/usr/bin/python2.7和/usr/bin/python3.5
    添加这两个版本

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

    还需要添加我们自己编译的版本,前面设置的安装路径是/usr/local/python3.7,
    因此完整的文件路径为/usr/local/python3.7/bin/python3.7

    sudo update-alternatives --install /usr/bin/python python /usr/local/python3.7/bin/python3.7 3
    
    • 1

    2.1.3 再次列出可用的 Python 版本

    leo@xlx:~$ update-alternatives --list python 
    /usr/bin/python2.7
    /usr/bin/python3.5
    
    • 1
    • 2
    • 3

    2.2 切换Python 版本

    运行sudo update-alternatives --config python,然后输入对应的序号,比如3,切换到3.7

    leo@xlx:~$ sudo update-alternatives --config python 
    There are 3 choices for the alternative python (providing /usr/bin/python).
    
      Selection    Path                                Priority   Status
    ------------------------------------------------------------
      0            /usr/local/python3.7/bin/python3.7   3         auto mode
    * 1            /usr/bin/python2.7                   1         manual mode
      2            /usr/bin/python3.5                   2         manual mode
      3            /usr/local/python3.7/bin/python3.7   3         manual mode
    
    Press <enter> to keep the current choice[*], or type selection number: 3
    update-alternatives: using /usr/local/python3.7/bin/python3.7 to provide /usr/bin/python (python) in manual mode
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3.查看当前版本

    运行python --version或者直接运行python查看版本信息
    已经切换到3.7

    leo@xlx:~$ python
    Python 3.7.1rc2 (default, Aug  3 2022, 21:42:46) 
    [GCC 5.4.0 20160609] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    leo@xlx:~$ python --version
    Python 3.7.1rc2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    html+css+js实现简单的交互效果
    【RocketMQ】消息的存储设计
    C++ Reference: Standard C++ Library reference: C Library: cuchar
    Springboot实现jwt的token验证(超简单)
    【C语言刷题】快慢指针巧解带环单链表问题
    重要的 SQL Server 函数 - 数字函数
    Dubbo: 基于SpringBoot+Dubbo的Provider/Consumer的实践
    Controller接收Postman的raw参数时,属性值全部为空
    ✊构建浏览器工作原理知识体系(浏览器内核篇)
    借助云的力量,重塑企业的现在和未来|re:Invent 2022 Adam Selipsky 主题演讲精华全收录
  • 原文地址:https://blog.csdn.net/aatu/article/details/126149088