centos7 安裝pyenv管理python版本

1、安裝pyenv的依賴,不然安裝python版本的時候可能會報錯

sudo yum install @development zlib-devel bzip2 bzip2-devel readline-devel sqlite \
sqlite-devel openssl-devel xz xz-devel libffi-devel findutils

2、安裝pyenv

從github上進行安裝即可

$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

安裝後需要配置path,將pyenv文件添加到系統路徑中,其原理是修改bash啓動文件,每次建立ssh的時候會自動運行。具體配置如下:

## 配置
root@pts/2 $ vim ~/.bashrc 
    # .bashrc
    
    # User specific aliases and functions
    
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    
    ## add by lilin at 20191015
    export PYENV_ROOT="$HOME/.pyenv"
    export PATH="$PYENV_ROOT/bin:$PATH"
    eval "$(pyenv init -)"
    
    # Source global definitions
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi

保存後需要使得配置生效,有如下三種做法:

## 配置生效
    ### 方式一
    重新打開一個session
    ### 方式二
    source ~/.bashrc
    ### 方式三
    exec $SHELL -l

輸入pyenv命令,如下顯示錶示安裝成功

[root@iZbp18vvdgiezb7zecn0dmZ ~]# pyenv 
pyenv 1.2.13
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands    List all available pyenv commands
   local       Set or show the local application-specific Python version
   global      Set or show the global Python version
   shell       Set or show the shell-specific Python version
   install     Install a Python version using python-build
   uninstall   Uninstall a specific Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   version     Show the current Python version and its origin
   versions    List all Python versions available to pyenv
   which       Display the full path to an executable
   whence      List all Python versions that contain the given executable

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme

使用命令查詢當前系統中存在的python版本

[root@iZbp18vvdgiezb7zecn0dmZ ~]# pyenv versions
* system (set by /root/.pyenv/version)

當前只存在系統默認的python版本,centos中默認是Python2.7

3、安裝多個版本的python,這裏我們安裝3.7.4

首先安裝解壓縮依賴,如果沒有,會無法解壓下載的python安裝文件導致報錯

sudo yum  -y install zlib*

使用pyenv install -l 或者pyenv install --list查看可以安裝的python版本,發現python已經提供很多版本,包括3.7.4,使用如下命令安裝對應版本

[root@iZbp18vvdgiezb7zecn0dmZ ~]# pyenv install 3.7.4 
WARNING: Please make sure you remove any previous custom paths from your /root/.pydistutils.cfg file.
Installing Python-3.7.4...
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
Installed Python-3.7.4 to /root/.pyenv/versions/3.7.4

[root@iZbp18vvdgiezb7zecn0dmZ ~]# pyenv versions
* system (set by /root/.pyenv/version)
  3.7.4

可以看到,成功安裝了3.7.4版本的python,當前激活的是系統默認版本。
小技巧1:這裏可以使用tab自動補全版本名字,比如輸入pyenv install 3.7.,按tab即可顯示匹配成功的版本。

[root@iZbp18vvdgiezb7zecn0dmZ ~]# pyenv install 3.7
3.7.0     3.7.1     3.7.2     3.7.3     3.7.4     3.7.5rc1  3.7-dev   
[root@iZbp18vvdgiezb7zecn0dmZ ~]# pyenv install 3.7.
3.7.0     3.7.1     3.7.2     3.7.3     3.7.4     3.7.5rc1

小技巧2:網絡環境比較差的時候,可以建立緩存文件夾,將對應版本的python安裝包下載到緩存文件夾中,再使用安裝命令時會自動在緩存文件夾中查找,一旦命中,就不用再去下載(特別是某些網絡環境特別差或者訪問國外網站網速慢的情況下),會直接安裝。

[root@iZbp18vvdgiezb7zecn0dmZ ~]# cd ~/.pyenv/cache/
[root@iZbp18vvdgiezb7zecn0dmZ ~]# mkdir cache
[root@iZbp18vvdgiezb7zecn0dmZ ~]# cd cache/
[root@iZbp18vvdgiezb7zecn0dmZ cache]# ll
total 0
[root@iZbp18vvdgiezb7zecn0dmZ cache]# wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz
--2019-10-15 15:13:20--  https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz
Resolving www.python.org (www.python.org)... 151.101.228.223, 2a04:4e42:1a::223
Connecting to www.python.org (www.python.org)|151.101.228.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 17131432 (16M) [application/octet-stream]
Saving to: 鈥楶ython-3.7.4.tar.xz鈥

100%[=============================================================================================================================>] 17,131,432  17.7KB/s   in 18m 53s

2019-10-15 15:32:15 (14.8 KB/s) - 鈥楶ython-3.7.4.tar.xz鈥saved [17131432/17131432]

4、常用命令

完整命令請參考官方文檔

安裝

pyenv install -l|--list # 列出所有可安裝的Python版本
pyenv install <version> # 安裝某個Python

卸載
卸載某個Python,-f參數指定是否強制卸載,如果強制卸載的話不會彈出提示,而且如果版本不存在的話也不會顯示錯誤信息:

pyenv uninstall [-f|--force] <version>

versions
列出所有已安裝的Python,當前使用的Python會用星號標出:

$ pyenv versions
  2.5.6
  2.6.8
* 2.7.6 (set by /home/yyuu/.pyenv/version)
  3.3.3
  jython-2.5.3
  pypy-2.2.1

global
通過寫~/.pyenv/version文件的方式設置全局Python:

$ pyenv global 2.7.6

local
通過在當前目錄寫.python-version文件的方式設置當前目錄下的Python:

pyenv local 2.7.6

shell
指定當前shell使用的Python:

$ pyenv shell pypy-2.2.1

當不再需要的時候,用–set來清除:

$ pyenv shell --unset

5、常見問題

用pyenv安裝Python的時候可能會出現各種各樣問題,例如缺少zlib、缺少ctypes模塊等等。對此pyenv也有專門一個頁面解決。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章