1.安裝Homebrew
通過終端下載並安裝Homebrew,在終端輸入:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
檢測Homebrew是否安裝成功,在終端輸入:
brew -v
若安裝正確,終端中會顯示當前安裝的Homebrew版本信息:
Homebrew 1.1.9
Homebrew/homebrew-core (git revision 9a2f; last commit 2017-02-07)
2.安裝pyenv
通過Homebrew下載並安裝pyenv,在終端輸入:
brew install pyenv
檢測pyenv是否安裝成功,在終端輸入:
pyenv -v
若安裝正確,終端中會顯示當前安裝的pyenv版本信息:
pyenv 1.0.7
3.安裝Python
通過pyenv查看可安裝的Python版本,在終端輸入:
pyenv install --list
通過pyenv安裝所需版本的Python,在終端輸入:
pyenv install <version>
以安裝Python-3.6.0為例,在終端輸入:
pyenv install 3.6.0
通過pyenv查看已安裝過的Python版本,在終端輸入:
pyenv versions
若安裝正確,終端中會顯示當前安裝的Python版本信息:
注:系統自帶版本的Python顯示為system,當前正在使用的Python版本前有星號作為標識
* system (set by /Users/caiyutong/.pyenv/version)
3.6.0
4.解決可能會出現的“*** [install] Error 1”錯誤
注:如果沒有出現此錯誤請跳過此步驟
在執行通過pyenv安裝所需版本的Python命令行后,可能會出現以下錯誤:
zipimport.ZipImportError: can't decompress data; zlib not available make: *** [install] Error 1
下載並安裝命令行工具,在終端輸入:
xcode-select --install
再次使用pyenv安裝所需版本的Python,在終端輸入:
pyenv install <version>
以安裝Python-3.6.0為例,在終端輸入:
pyenv install 3.6.0
通過pyenv查看已安裝過的Python版本,在終端輸入:
pyenv versions
若安裝正確,終端中會顯示當前安裝的Python版本信息:
注:系統自帶版本的Python顯示為system,當前正在使用的Python版本前有星號作為標識
* system (set by /Users/caiyutong/.pyenv/version)
3.6.0
5.編輯.bash_profile文件
切換到用戶的Home目錄,在終端輸入:
cd ~
打開.bash_profile文件,在終端輸入:
open .bash_profile
若文件不存在,創建.bash_profile文件,在終端輸入:
touch .bash_profile
打開並編輯.bash_profile文件,在終端輸入:
open -e .bash_profile
在彈出的.bash_profile文件中增加以下內容,並按command + s保存文件:
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
使用更新之后的.bash_profile內容,在終端輸入:
source .bash_profile
6.切換全局的python版本
注:此步驟將系統全局的Python版本切換為指定版本,只切換目標目錄的Python版本請看步驟7
將全局的Python切換為指定版本,在終端輸入:
pyenv global <version>
以切換至Python-3.6.0為例,在終端輸入:
pyenv global 3.6.0
檢測是否切換成功,在終端輸入:
python
若切換成功則顯示相應版本Python,退出Python返回終端操作,在“>>>”后輸入:
exit(0)
將全局的Python切換回系統自帶版本,在終端輸入:
pyenv global system
7.切換目標目錄的Python版本
注:此步驟將目標目錄的Python版本切換為指定版本,需切換系統全局的Python版本請看步驟6
切換到指定的目錄,在終端輸入:
cd <directory>
以切換至/Users/caiyutong/Documents/workspace for python目錄為例,在終端輸入:
cd /Users/caiyutong/Documents/"workspace for python"
將當前目錄下的Python切換為指定版本,在終端輸入:
pyenv local <version>
以切換至Python-3.6.0為例,在終端輸入:
pyenv local 3.6.0
檢測是否切換成功,在終端輸入:
python
若切換成功則顯示相應版本Python,退出Python返回終端操作,在“>>>”后輸入:
exit(0)
將全局的Python切換回系統自帶版本,在終端輸入:
pyenv local system
附1:如何正確的在終端操作名稱中含有空格的目錄
注:若要對有空格的文件夾進行操作的話在文件夾名兩邊加單引號或雙引號,或在文件夾名中的空格前加“\”
將工作目錄切換到/Users/caiyutong/Documents/workspace for python下,不可行的方法:
cd /Users/caiyutong/Documents/workspace for python
如果在/Users/caiyutong/Documents目錄下創建過workspace目錄,則上述操作等同於:
cd /Users/caiyutong/Documents/workspace
如果在/Users/caiyutong/Documents目錄下沒有創建過workspace目錄,則系統提示錯誤:
-bash: cd: /Users/caiyutong/Documents/workspace: No such file or directory)
可行的辦法之一,在含有空格的文件夾名兩邊加單引號:
cd /Users/caiyutong/Documents/'workspace for python'
可行的辦法之二,在含有空格的文件夾名兩邊加雙引號:
cd /Users/caiyutong/Documents/"workspace for python"
可行的辦法之三,在在文件夾名中的空格前加反斜杠:
cd /Users/caiyutong/Documents/workspace\ for\ python
附2:關於pyenv的功能
若想要了解pyenv可以進行的操作,可在終端輸入:
pyenv help
在終端列舉出的pyenv可執行命令如下:
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
參考資料1:https://www.zhihu.com/question/53335978
(關於如何下載並使用pyenv安裝Python)
參考資料2:https://github.com/yyuu/pyenv/issues/454
(關於如何解決用pyenv安裝Python時所遇到的“*** [install] Error 1”錯誤)
參考資料3:https://zhidao.baidu.com/question/490173412010484492.html
(關於如何在終端正確操作名稱中含有空格的目錄)
參考資料4:http://www.tuicool.com/articles/EZJRbmB
(關於pyenv help)
