1. CentOS 7 系統(tǒng)下使用 Pyenv 安裝 Python 環(huán)境
1.1. Pyenv 介紹
??Pyenv 是一款解決 Python 多版本環(huán)境使用的管理工具斤富,它源自 rbenv 和 ruby-build 熙暴。通過 pyenv 我們可以同時編譯安裝部署多個 Python 環(huán)境狈茉,方便多 python 版本開發(fā)調試項目亮瓷。詳細介紹可以訪問 pyenv 項目主頁:https://github.com/pyenv/pyenv
1.2. Linux 環(huán)境下安裝 Pyenv
??安裝 pyenv 比較簡單,僅需將 pyenv 項目下載到當前用戶家目錄朴摊,然后配置指定的環(huán)境變量后即可使用句占。由于 pyenv 項目存放在 github 上,因此我們可以使用 git 命令 clone 到用戶家目錄诱篷。下面我們在 CentOS 7 系統(tǒng)上用 python 用戶演示一下安裝的具體過程:
[python@localhost ~]$ su -l root
Password:
Last login: Tue Apr 2 16:07:47 CST 2019 on pts/0
[root@localhost ~]# yum install git
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: mirrors.cn99.com
* updates: mirrors.cn99.com
Package git-1.8.3.1-20.el7.x86_64 already installed and latest version
Nothing to do
[root@localhost ~]# exit
logout
[python@localhost ~]$ git clone https://github.com/pyenv/pyenv.git .pyenv
Cloning into '.pyenv'...
remote: Enumerating objects: 81, done.
remote: Counting objects: 100% (81/81), done.
remote: Compressing objects: 100% (45/45), done.
remote: Total 16818 (delta 37), reused 61 (delta 28), pack-reused 16737
Receiving objects: 100% (16818/16818), 3.29 MiB | 463.00 KiB/s, done.
Resolving deltas: 100% (11379/11379), done.
[python@localhost ~]$ ls -adl .pyenv
drwxrwxr-x. 11 python python 4096 Apr 2 16:13 .pyenv
[python@localhost ~]$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
[python@localhost ~]$
[python@localhost ~]$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
[python@localhost ~]$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
[python@localhost ~]$ pyenv
bash: pyenv: command not found...
[python@localhost ~]$
[python@localhost ~]$ source .bashrc
[python@localhost ~]$ pyenv
pyenv 1.2.9-35-gb610909
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
上圖中成功安裝完成 Pyenv
1.3. 使用 pyenv 編譯安裝 Python 環(huán)境
??pyenv 的基礎功能就是安裝不同版本的 Python ,具體的選項為 install
除了 install 選項以外雳灵,還有其他選項棕所,詳情可參考 Pyenv 選項文檔 。使用 pyenv install --list
可以列出當前 pyenv 工具支持的 Python 版本悯辙,將 --list 改為具體的版本號后琳省,可以完成具體 Python 環(huán)境的安裝, 下面我們演示一下:
[python@localhost ~]$ pyenv install --list
Available versions:
2.1.3
...
省略部分具體版本
...
3.5.7
3.6.0
3.6-dev
3.6.1
3.6.2
3.6.3
3.6.4
3.6.5
3.6.6
3.6.7
...
省略部分具體版本
...
[python@localhost ~]$
[python@localhost ~]$ pyenv install -v 3.6.3
/tmp/python-build.20190402172510.109803 ~
Downloading Python-3.6.3.tar.xz...
-> https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz
/tmp/python-build.20190402172510.109803/Python-3.6.3 /tmp/python-build.20190402172510.109803 ~
Installing Python-3.6.3...
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.6... no
checking for python3... no
checking for python... python
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... linux
checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/tmp/python-build.20190402172510.109803/Python-3.6.3`:
configure: error: no acceptable C compiler found in $PATH
See `config.log` for more details
BUILD FAILED (CentOS Linux 7 using python-build 1.2.9-35-gb610909)
Inspect or clean up the working tree at /tmp/python-build.20190402172510.109803
Results logged to /tmp/python-build.20190402172510.109803.log
Last 10 log lines:
checking for --with-universal-archs... no
checking MACHDEP... linux
checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/tmp/python-build.20190402172510.109803/Python-3.6.3`:
configure: error: no acceptable C compiler found in $PATH
See `config.log` for more details
??上圖中使用 Pyenv install -v 3.6.3
提示編譯安裝的錯誤信息躲撰,這是因為 Pyenv 底層安裝 Python 環(huán)境是通過編譯安裝實現的针贬,而我的系統(tǒng)中并未提供編譯安裝所依賴的軟件包。那好拢蛋,下面我們先在系統(tǒng)中安裝完成編譯 python 所依賴的所有軟件包桦他,如圖:
[python@localhost ~]$ su -l root
Password:
Last login: Tue Apr 2 16:24:02 CST 2019 on :0
[root@localhost ~]#
[root@localhost ~]# yum install -y gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel ncurses-devel libffi-devel
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: mirrors.cn99.com
* updates: mirrors.cn99.com
Package gcc-4.8.5-36.el7_6.1.x86_64 already installed and latest version
Package 1:make-3.82-23.el7.x86_64 already installed and latest version
Package patch-2.7.1-10.el7_5.x86_64 already installed and latest version
Package gdbm-devel-1.10-8.el7.x86_64 already installed and latest version
Package 1:openssl-devel-1.0.2k-16.el7_6.1.x86_64 already installed and latest version
Package sqlite-devel-3.7.17-8.el7.x86_64 already installed and latest version
Package readline-devel-6.2-10.el7.x86_64 already installed and latest version
Package zlib-devel-1.2.7-18.el7.x86_64 already installed and latest version
Package bzip2-devel-1.0.6-13.el7.x86_64 already installed and latest version
Package ncurses-devel-5.9-14.20130511.el7_4.x86_64 already installed and latest version
Package libffi-devel-3.0.13-18.el7.x86_64 already installed and latest version
Nothing to do
[root@localhost ~]# exit
logout
[python@localhost ~]$
[python@localhost ~]$ pyenv install -v 3.6.3
/tmp/python-build.20190402174323.1818 ~
Downloading Python-3.6.3.tar.xz...
-> https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz
...
省略編譯安裝具體過程
...
Installing collected packages: setuptools, pip
Successfully installed pip-9.0.1 setuptools-28.8.0
Installed Python-3.6.3 to /home/python/.pyenv/versions/3.6.3
/tmp/python-build.20190402174323.1818 ~
~
[python@localhost ~]$
??上圖中順利完成 python3.6.3 這個版本的安裝工作。但是有的時候網絡不好谆棱,下載源碼包比較慢這時我們可以在 .pyenv 目錄內創(chuàng)建 cache 文件夾快压,然后使用內網的 ftp 服務器下載指定的 python版本,然后再使用 pyenv install 安裝础锐,這樣可以節(jié)省時間,如圖:
[python@localhost ~]$ cd .pyenv/
[python@localhost .pyenv]$ ls
bin completions LICENSE pyenv.d src versions
CHANGELOG.md CONDUCT.md Makefile README.md terminal_output.png
COMMANDS.md libexec plugins shims test
[python@localhost .pyenv]$
[python@localhost .pyenv]$ mkdir cache
[python@localhost .pyenv]$ cd cache/
[python@localhost cache]$
[python@localhost cache]$ lftp 172.20.0.1/pub/software
cd ok, cwd=/pub/software
lftp 172.20.0.1:/pub/software> ls
-rw-r--r-- 1 0 0 15324736 Mar 18 00:57 Python-3.5.7.tar.xz
-rw-r--r-- 1 0 0 22673115 Oct 03 2017 Python-3.6.3.tgz
-rw-r--r-- 1 0 0 17212420 Dec 24 03:02 Python-3.6.8.tar.xz
-rw-r--r-- 1 0 0 17108364 Mar 25 20:59 Python-3.7.3.tar.xz
-rw-r--r-- 1 0 0 332489780 Apr 03 12:38 pycharm-community-2019.1.1.tar.gz
lftp 172.20.0.1:/pub/software> mget Python*
72318635 bytes transferred in 1 second (63.98M/s) Total 4 files transferred
lftp 172.20.0.1:/pub/software> exit
[python@localhost cache]$ ll
total 70632
-rw-rw-r--. 1 python python 15324736 Mar 18 08:57 Python-3.5.7.tar.xz
-rw-rw-r--. 1 python python 22673115 Oct 3 2017 Python-3.6.3.tgz
-rw-rw-r--. 1 python python 17212420 Dec 24 11:02 Python-3.6.8.tar.xz
-rw-rw-r--. 1 python python 17108364 Mar 26 04:59 Python-3.7.3.tar.xz
[python@localhost cache]$ cd
??完成上面的操作后荧缘,再次使用 pyenv install 命令安裝對應的 python 版本時皆警,會跳過下載軟件包的過程,直接使用 cache 路徑下的源碼包截粗。
??上面我們演示了通過 pyenv 成功的安裝了 python3.6.3信姓,那我們如何查看剛安裝完成的 python 呢 鸵隧?下面介紹一下 Pyenv 的 versions 選項:
[python@localhost ~]$ pyenv versions --help
Usage: pyenv versions [--bare] [--skip-aliases]
Lists all Python versions found in `$PYENV_ROOT/versions/*'.
[python@localhost ~]$
??versions 可以列出 pyenv 已安裝的所有 python 版本,具體位置在 .pyenv 的 versions 文件夾下意推,我們可以進入該文件里查看一下豆瘫,如圖:
[python@localhost ~]$ pwd
/home/python
[python@localhost ~]$ cd .pyenv/
[python@localhost .pyenv]$ ll
total 204
drwxrwxr-x. 2 python python 4096 Apr 2 16:27 bin
-rw-rw-r--. 1 python python 25848 Apr 2 16:27 CHANGELOG.md
-rw-rw-r--. 1 python python 7522 Apr 2 16:27 COMMANDS.md
drwxrwxr-x. 2 python python 4096 Apr 2 16:27 completions
-rw-rw-r--. 1 python python 3390 Apr 2 16:27 CONDUCT.md
drwxrwxr-x. 2 python python 4096 Apr 2 16:27 libexec
-rw-rw-r--. 1 python python 1092 Apr 2 16:27 LICENSE
-rw-rw-r--. 1 python python 406 Apr 2 16:27 Makefile
drwxrwxr-x. 3 python python 4096 Apr 2 16:27 plugins
drwxrwxr-x. 4 python python 4096 Apr 2 16:27 pyenv.d
-rw-rw-r--. 1 python python 15752 Apr 2 16:27 README.md
drwxrwxr-x. 2 python python 4096 Apr 2 17:46 shims
drwxrwxr-x. 2 python python 4096 Apr 2 16:27 src
-rw-rw-r--. 1 python python 104764 Apr 2 16:27 terminal_output.png
drwxrwxr-x. 3 python python 4096 Apr 2 16:27 test
drwxrwxr-x. 3 python python 4096 Apr 2 17:43 versions
[python@localhost .pyenv]$ cd versions/
[python@localhost versions]$ ll
total 4
drwxr-xr-x. 6 python python 4096 Apr 2 17:46 3.6.3
[python@localhost versions]$ cd 3.6.3/
[python@localhost 3.6.3]$ ll
total 16
drwxr-xr-x. 2 python python 4096 Apr 2 17:46 bin
drwxr-xr-x. 3 python python 4096 Apr 2 17:46 include
drwxr-xr-x. 4 python python 4096 Apr 2 17:46 lib
drwxr-xr-x. 3 python python 4096 Apr 2 17:46 share
[python@localhost 3.6.3]$ cd bin/
[python@localhost bin]$ ll
total 24808
lrwxrwxrwx. 1 python python 8 Apr 2 17:46 2to3 -> 2to3-3.6
-rwxrwxr-x. 1 python python 125 Apr 2 17:46 2to3-3.6
lrwxrwxrwx. 1 python python 16 Apr 2 17:46 easy_install -> easy_install-3.6
-rwxrwxr-x. 1 python python 266 Apr 2 17:46 easy_install-3.6
lrwxrwxrwx. 1 python python 7 Apr 2 17:46 idle -> idle3.6
lrwxrwxrwx. 1 python python 7 Apr 2 17:46 idle3 -> idle3.6
-rwxrwxr-x. 1 python python 123 Apr 2 17:46 idle3.6
lrwxrwxrwx. 1 python python 6 Apr 2 17:46 pip -> pip3.6
-rwxrwxr-x. 1 python python 238 Apr 2 17:46 pip3
-rwxrwxr-x. 1 python python 238 Apr 2 17:46 pip3.6
lrwxrwxrwx. 1 python python 8 Apr 2 17:46 pydoc -> pydoc3.6
lrwxrwxrwx. 1 python python 8 Apr 2 17:46 pydoc3 -> pydoc3.6
-rwxrwxr-x. 1 python python 108 Apr 2 17:46 pydoc3.6
lrwxrwxrwx. 1 python python 9 Apr 2 17:46 python -> python3.6
lrwxrwxrwx. 1 python python 9 Apr 2 17:46 python3 -> python3.6
-rwxr-xr-x. 2 python python 12650352 Apr 2 17:45 python3.6
lrwxrwxrwx. 1 python python 17 Apr 2 17:46 python3.6-config -> python3.6m-config
-rwxr-xr-x. 1 python python 63994 Apr 2 17:46 python3.6-gdb.py
-rwxr-xr-x. 2 python python 12650352 Apr 2 17:45 python3.6m
-rwxr-xr-x. 1 python python 3141 Apr 2 17:46 python3.6m-config
lrwxrwxrwx. 1 python python 16 Apr 2 17:46 python3-config -> python3.6-config
lrwxrwxrwx. 1 python python 16 Apr 2 17:46 python-config -> python3.6-config
lrwxrwxrwx. 1 python python 10 Apr 2 17:46 pyvenv -> pyvenv-3.6
-rwxrwxr-x. 1 python python 465 Apr 2 17:46 pyvenv-3.6
[python@localhost bin]$ cd
??通過上圖,我們發(fā)現菊值,pyenv 將編譯安裝的具體版本 python 程序直接安裝到了 pyenv 的versions 目錄下外驱。 下面我們使用 pyenv 的 versions 選項命令 查看 pyenv 下都安裝了哪些具體的版本,如圖:
[python@localhost ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.3
[python@localhost ~]$ python -V
Python 2.7.5
[python@localhost ~]$
??上圖中 pyenv versions 命令執(zhí)行后返回了兩個 python 版本腻窒,system 代表 CentOS 7 系統(tǒng)自帶的 python2.7 版本昵宇, 3.6.3 代表我們之前通過 pyenv 安裝的 python3.6.3 版本。system 前面的 *
代表當前 shell 環(huán)境生效的 python 版本儿子。
??現在我們可以方便的使用 pyenv 安裝各種版本瓦哎,當然之后也可以使用 uninstall 選項刪除之前安裝的具體版本,比如我們先安裝一個 python3.6.6 然后再使用 pyenv uninstall 3.6.3
卸載 Python3.6.3 版本,演示過程如下:
[python@localhost ~]$ pyenv install 3.6.6
Downloading Python-3.6.6.tar.xz...
-> https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz
Installing Python-3.6.6...
Installed Python-3.6.6 to /home/python/.pyenv/versions/3.6.6
[python@localhost ~]$
[python@localhost ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.3
3.6.6
[python@localhost ~]$ pyenv uninstall 3.6.3
pyenv: remove /home/python/.pyenv/versions/3.6.3? y
[python@localhost ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.6
[python@localhost ~]$
1.4. 使用 pyenv global 指定系統(tǒng) python 環(huán)境
??默認情況下 pyenv 顯示的是系統(tǒng)默認的環(huán)境變量 2.7.5 版本柔逼,我們可以使用 global 選項來指定我們想要 python 版本蒋譬, 如圖:
[python@localhost ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.3
3.6.8
3.7.3
[python@localhost ~]$ python -V
Python 2.7.5
[python@localhost ~]$ pyenv global 3.6.8
[python@localhost ~]$ pyenv versions
system
3.6.3
* 3.6.8 (set by /home/python/.pyenv/version)
3.7.3
[python@localhost ~]$ python -V
Python 3.6.8
[python@localhost ~]$
??通過上面的演示,我們發(fā)現愉适,使用 global 選項指定當前 python 版本是通過在 .pyenv
目錄中的 version 文件指定的犯助,下面我們看一下這個文件:
[python@localhost ~]$ cd .pyenv/
[python@localhost .pyenv]$ ll
total 208
drwxrwxr-x. 2 python python 4096 Apr 2 20:22 bin
-rw-rw-r--. 1 python python 25848 Apr 2 20:22 CHANGELOG.md
-rw-rw-r--. 1 python python 7522 Apr 2 20:22 COMMANDS.md
drwxrwxr-x. 2 python python 4096 Apr 2 20:22 completions
-rw-rw-r--. 1 python python 3390 Apr 2 20:22 CONDUCT.md
drwxrwxr-x. 2 python python 4096 Apr 2 20:22 libexec
-rw-rw-r--. 1 python python 1092 Apr 2 20:22 LICENSE
-rw-rw-r--. 1 python python 406 Apr 2 20:22 Makefile
drwxrwxr-x. 3 python python 4096 Apr 2 20:22 plugins
drwxrwxr-x. 4 python python 4096 Apr 2 20:22 pyenv.d
-rw-rw-r--. 1 python python 15752 Apr 2 20:22 README.md
drwxrwxr-x. 2 python python 4096 Apr 3 10:38 shims
drwxrwxr-x. 2 python python 4096 Apr 2 20:22 src
-rw-rw-r--. 1 python python 104764 Apr 2 20:22 terminal_output.png
drwxrwxr-x. 3 python python 4096 Apr 2 20:22 test
-rw-rw-r--. 1 python python 6 Apr 3 14:55 version
drwxrwxr-x. 5 python python 4096 Apr 2 20:43 versions
[python@localhost .pyenv]$ cat version
3.6.8
[python@localhost .pyenv]$
??通過上圖的查看過程,發(fā)現 version 就是一個普通文本文件儡毕,內部指定了3.6.8這個編號也切,下面我們將這個文件修改一下,3.6.8 修改為 3.6.3 腰湾,或者把這個文件刪除雷恃,看看當前的 python 環(huán)境有什么變化,如圖:
[python@localhost .pyenv]$ cd
[python@localhost ~]$ echo "3.6.3" > .pyenv/version
[python@localhost ~]$ cat .pyenv/version
3.6.3
[python@localhost ~]$ pyenv versions
system
* 3.6.3 (set by /home/python/.pyenv/version)
3.6.8
3.7.3
[python@localhost ~]$ python -V
Python 3.6.3
[python@localhost ~]$ rm -rf .pyenv/version
[python@localhost ~]$ cat .pyenv/version
cat: .pyenv/version: No such file or directory
[python@localhost ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.3
3.6.8
3.7.3
[python@localhost ~]$ python -V
Python 2.7.5
[python@localhost ~]$
??上圖的演示過程充分揭示了 global 選項指定當前 python 版本的方法费坊,沒有 version 文件的情況下倒槐,默認使用系統(tǒng)中自帶的 python 版本, 有了 version 文件后附井,使用 version 文件中指定的 python 版本讨越。
??之前對 global 的用法已經可以滿足通常的使用場景,但是有的時候我們可以使用 global 的高級用法永毅,我們看一下下面的執(zhí)行過程把跨,可以發(fā)現一些新的特性:
[python@localhost ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.3
3.6.8
3.7.3
[python@localhost ~]$ python -V
Python 2.7.5
[python@localhost ~]$ python2.7 -V
Python 2.7.5
[python@localhost ~]$ python3 -V
pyenv: python3: command not found
The `python3` command exists in these Python versions:
3.6.3
3.6.8
3.7.3
[python@localhost ~]$ python3.6 -V
pyenv: python3.6: command not found
The `python3.6` command exists in these Python versions:
3.6.3
3.6.8
[python@localhost ~]$ python3.6.3 -V
bash: python3.6.3: command not found...
[python@localhost ~]$
??上圖中沒有設置 global 環(huán)境,系統(tǒng)默認為 python2.7.5 沼死,在命令行中執(zhí)行 python 或者 python2.7 時着逐,最終都使用的是 python2.7.5 ; 當執(zhí)行 python3 或者 python3.x 時,提示 pyenv 中有對應版本;直接指定對應的系統(tǒng)版本時,若不在 version 文件中耸别,則提示命令找不到健芭,無法使用。因此我們可以使用 global 同時定義多個 python3 系列秀姐,然后同時使用多個版本慈迈,如下圖:
[python@localhost ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.3
3.6.8
3.7.3
[python@localhost ~]$ cat .pyenv/version
cat: .pyenv/version: No such file or directory
[python@localhost ~]$ pyenv global 3.6.8 3.6.3 3.7.3
[python@localhost ~]$ pyenv versions
system
* 3.6.3 (set by /home/python/.pyenv/version)
* 3.6.8 (set by /home/python/.pyenv/version)
* 3.7.3 (set by /home/python/.pyenv/version)
[python@localhost ~]$ python -V
Python 3.6.8
[python@localhost ~]$ python3 -V
Python 3.6.8
[python@localhost ~]$ python3.6 -V
Python 3.6.8
[python@localhost ~]$ python3.7 -V
Python 3.7.3
[python@localhost ~]$
??上圖中我們連續(xù)設置了3個 python 版本,發(fā)現他們同時生效省有,但是最終python命令生效的版本是 3.6.8 即我們 global 變量后第一個 python版本 3.6.8 痒留; python3 和 python3.6 都為 3.6.8;如果直接執(zhí)行python 3.7锥咸,則使用的是 python3.7.3狭瞎;高級用法大家了解一下就好,盡量不要把自己的 python 環(huán)境搞得太復雜搏予。
1.5. 使用 pyenv shell 指定系統(tǒng) python 環(huán)境
??除了使用上一小節(jié)中的 global 選項外熊锭,我們還可以通過 shell 選項設置當前系統(tǒng)的環(huán)境變量,和 global 用法相仿雪侥,下面我們演示一下:
[python@localhost ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.3
3.6.8
3.7.3
[python@localhost ~]$ pyenv shell 3.6.3
[python@localhost ~]$ pyenv versions
system
* 3.6.3 (set by PYENV_VERSION environment variable)
3.6.8
3.7.3
[python@localhost ~]$ echo $PYENV_VERSION
3.6.3
[python@localhost ~]$ PYENV_VERSION=3.6.8
[python@localhost ~]$
[python@localhost ~]$ pyenv versions
system
3.6.3
* 3.6.8 (set by PYENV_VERSION environment variable)
3.7.3
[python@localhost ~]$ unset PYENV_VERSION
[python@localhost ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.3
3.6.8
3.7.3
[python@localhost ~]$
[python@localhost ~]$ export PYENV_VERSION=3.7.3
[python@localhost ~]$ pyenv versions
system
3.6.3
3.6.8
* 3.7.3 (set by PYENV_VERSION environment variable)
[python@localhost ~]$
??通過上圖的演示過程碗殷,我們可以使用 shell 選項指定當前系統(tǒng)的 python 版本, 我們發(fā)現 shell 選項方式和 global 不同速缨,他將 python 的版本信息放到當前 shell 環(huán)境變量 PYENV_VERSION 中锌妻, 我們可以直接操作該變量,通過改變他的值實現 python 不同版本的切換旬牲,也可以直接使用 Linux 中的 unset 命令刪除這個變量仿粹。刪除后,pyenv 恢復到 global 所指定的 python 版本原茅,這也說明 shell 選項設置的 python 版本優(yōu)先級高于 global 設置的吭历。雖然大家了解了 shell 選項的實現原理,但是還是建議大家使用 pyenv 命令來維護系統(tǒng)中的 python 版本擂橘,不建議直接操作對應的系統(tǒng)變量晌区。比如,我們可以使用 --unset 來取消 shell 選項設置的 python 版本通贞,如圖:
[python@localhost ~]$ pyenv global 3.7.3
[python@localhost ~]$ pyenv versions
system
3.6.3
3.6.8
* 3.7.3 (set by /home/python/.pyenv/version)
[python@localhost ~]$ pyenv shell 3.6.8
[python@localhost ~]$ pyenv versions
system
3.6.3
* 3.6.8 (set by PYENV_VERSION environment variable)
3.7.3
[python@localhost ~]$ pyenv shell --unset
[python@localhost ~]$ pyenv versions
system
3.6.3
3.6.8
* 3.7.3 (set by /home/python/.pyenv/version)
[python@localhost ~]$
??shell 選項和 global 選項類似朗若, 也可以同時制定多個 python 版本, 具體實現是在 PYENV_VERSION
環(huán)境變量中用 :
分隔多個 Python 版本號昌罩,如下圖:
[python@localhost ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.3
3.6.8
3.7.3
[python@localhost ~]$ pyenv shell 3.6.3 3.6.8 3.7.3
[python@localhost ~]$
[python@localhost ~]$ pyenv versions
system
* 3.6.3 (set by PYENV_VERSION environment variable)
* 3.6.8 (set by PYENV_VERSION environment variable)
* 3.7.3 (set by PYENV_VERSION environment variable)
[python@localhost ~]$
[python@localhost ~]$ echo $PYENV_VERSION
3.6.3:3.6.8:3.7.3
[python@localhost ~]$ python -V
Python 3.6.3
[python@localhost ~]$ python3.6 -V
Python 3.6.3
[python@localhost ~]$ python3.7 -V
Python 3.7.3
[python@localhost ~]$ pyenv shell --unset
[python@localhost ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.3
3.6.8
3.7.3
[python@localhost ~]$
1.6. 使用 pyenv local 指定系統(tǒng) python 環(huán)境
??下面給大家介紹最后一個可以改變系統(tǒng)環(huán)境變量的 pyenv 選項 local 哭懈,該選項可以指定當前路徑和其子目錄下的 Python 版本,即在指定目錄內執(zhí)行 python 時才有效的 python 版本茎用。演示如下:
[python@localhost ~]$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
[python@localhost ~]$ mkdir MyProject
[python@localhost ~]$ cd MyProject/
[python@localhost MyProject]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.3
3.6.8
3.7.3
[python@localhost MyProject]$ pyenv local 3.6.8
[python@localhost MyProject]$ pyenv versions
system
3.6.3
* 3.6.8 (set by /home/python/MyProject/.python-version)
3.7.3
[python@localhost MyProject]$ ls -al
total 12
drwxrwxr-x. 2 python python 4096 Apr 4 10:18 .
drwx------. 18 python python 4096 Apr 4 10:17 ..
-rw-rw-r--. 1 python python 6 Apr 4 10:18 .python-version
[python@localhost MyProject]$ cat .python-version
3.6.8
[python@localhost MyProject]$ cd
[python@localhost ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.3
3.6.8
3.7.3
[python@localhost ~]$
??現在我們演示一下 global shell local 三種選項同時使用遣总,觀察一下哪種的優(yōu)先級最高:
[python@localhost ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.6.3
3.6.8
3.7.3
[python@localhost ~]$ pyenv global 3.6.3
[python@localhost ~]$ pyenv versions
system
* 3.6.3 (set by /home/python/.pyenv/version)
3.6.8
3.7.3
[python@localhost ~]$ pyenv shell 3.6.8
[python@localhost ~]$ pyenv versions
system
3.6.3
* 3.6.8 (set by PYENV_VERSION environment variable)
3.7.3
[python@localhost ~]$ cd MyProject/
[python@localhost MyProject]$ pyenv local 3.7.3
[python@localhost MyProject]$ pyenv versions
system
3.6.3
* 3.6.8 (set by PYENV_VERSION environment variable)
3.7.3
[python@localhost MyProject]$ pyenv shell --unset
[python@localhost MyProject]$ pyenv versions
system
3.6.3
3.6.8
* 3.7.3 (set by /home/python/MyProject/.python-version)
[python@localhost MyProject]$ cd
[python@localhost ~]$ pyenv versions
system
* 3.6.3 (set by /home/python/.pyenv/version)
3.6.8
3.7.3
[python@localhost ~]$
??通過上面的演示你虹,我們發(fā)現 shell 選項定義的 python 版本優(yōu)先級最高,他會覆蓋 local 選項指定的python 版本彤避,然后 local 選項的優(yōu)先級高于 global 選項的優(yōu)先級。
??下面我們可以在 pycharm 中直接使用安裝在 .pyenv/versions 中安裝的各種 python 版本夯辖。