查看: 44|回覆: 0

[Ubuntu] Ubuntu 安装python3及多版本切换

[複製鏈接]

2

主題

0

回帖

0

積分

热心网友

金币
0
閲讀權限
220
精華
0
威望
0
贡献
0
在線時間
0 小時
註冊時間
2009-6-18
發表於 2021-1-5 12:08:00 | 顯示全部樓層 |閲讀模式

Ubuntu上python版本查看

$ python -V
Python 2.7.12

1.安装3.7

添加源

sudo add-apt-repository ppa:deadsnakes/ppa

检查更新

sudo apt-get update 

安装python3.7

sudo apt-get install python3.7

安装pip3

sudo apt install python3-pip

2.使用 update-alternatives 来为整个系统更改Python 版本

查看python替代版本信息

~$ update-alternatives --display python

但是结果为

update-alternatives: error: no alternatives for python

python的替代版本尚未被update-alternatives

查看python的位置

$ whereis python
python: /usr/bin/python2.7 /usr/bin/python3.7 /usr/bin/python3.5m /usr/bin/python3.7m /usr/bin/python3.5 /usr/bin/python /usr/lib/python2.7 /usr/lib/python3.7 /usr/lib/python3.5 /etc/python2.7 /etc/python3.7 /etc/python3.5 /etc/python /usr/local/lib/python2.7 /usr/local/lib/python3.7 /usr/local/lib/python3.5 /usr/share/python /usr/share/man/man1/python.1.gz

更新一下替代列表

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
baby@ubuntu:~$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2
update-alternatives: using /usr/bin/python3.7 to provide /usr/bin/python (python) in auto mode

查看python替代版本信息

$ update-alternatives --display python                             python - auto mode
  link best version is /usr/bin/python3.7
  link currently points to /usr/bin/python3.7
  link python is /usr/bin/python
/usr/bin/python2.7 - priority 1
/usr/bin/python3.7 - priority 2

查看python版本

$ python -V
Python 3.7.9

3.切换版本

切换到2.7

$ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.7   2         auto mode
  1            /usr/bin/python2.7   1         manual mode
  2            /usr/bin/python3.7   2         manual mode

Press <enter> to keep the current choice
  • , or type selection number: 1 update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in manual mode baby@ubuntu:~$ python -V Python 2.7.12
  • pip版本

    $ pip -V
    pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

    切换到3.7

    sudo update-alternatives --config python
    There are 2 choices for the alternative python (providing /usr/bin/python).
    
      Selection    Path                Priority   Status
    ------------------------------------------------------------
      0            /usr/bin/python3.7   2         auto mode
    * 1            /usr/bin/python2.7   1         manual mode
      2            /usr/bin/python3.7   2         manual mode
    
    Press <enter> to keep the current choice
  • , or type selection number: 2 update-alternatives: using /usr/bin/python3.7 to provide /usr/bin/python (python) in manual mode baby@ubuntu:~$ python -V Python 3.7.9
  • 安装pip

    sudo apt install python3-pip

    pip版本

     pip -V
    pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.7)

    pip的版本随着python版本切换



    来源:https://www.cnblogs.com/baby123/p/14234171.html
    回覆

    使用道具 舉報

    您需要登錄後才可以回帖 登錄 | 立即注册

    本版積分規則

    相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

    Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

    在本版发帖返回顶部