參考
注意
對于 .bash_profile 文件的修改机错,終端需要重新打開务唐,在執(zhí)行命令服球,才能看到修改生效的結(jié)果汛兜。
實踐
open ~/.bash_profile
打開這個文件.bash_profile劝篷,內(nèi)容如下
# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH
PATH="/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/bin:${PATH}"
export PATH
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
# added by Anaconda3 5.1.0 installer
export PATH="/Users/hncy-ios/anaconda3/bin:$PATH"
從上往下看哨鸭,依次是自己安裝的3.6版本的Python
/Library/Frameworks/Python.framework/Versions/3.6
安裝的anaconda3的Python版本
/Users/hncy-ios/anaconda3
此時沒有看到系統(tǒng)自帶的Python版本,終端執(zhí)行python娇妓,可以看到:
wxxdemac-mini:~ hncy-ios$ python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
終端執(zhí)行python3像鸡,可以看到:
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
看來是安裝了anaconda3,環(huán)境配置覆蓋了系統(tǒng)默認(rèn)的Python版本峡蟋,試著這樣注釋.bash_profile文件最后一行:
#export PATH="/Users/hncy-ios/anaconda3/bin:$PATH"
重新打開終端
終端執(zhí)行python坟桅,可以看到:
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
很棒,原來的環(huán)境回來了蕊蝗。
那么仅乓,系統(tǒng)默認(rèn)安裝路徑是在哪里?
通過這樣:
>>> import sys
>>> print('\n'.join(sys.path))
/Library/Python/2.7/site-packages/pip-9.0.3-py2.7.egg
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/Library/Python/2.7/site-packages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
>>>
可以看到蓬戚,這個就是系統(tǒng)默認(rèn)Python的安裝路徑
/System/Library/Frameworks/Python.framework/Versions/2.7
至于這個路徑在哪里配置呢夸楣,文件 .bash_profile 中第二行注釋是這么寫的:
# The original version is saved in .bash_profile.pysave
嘗試打開這個文件 .bash_profile.pysave,發(fā)現(xiàn)找不到這個文件子漩,為啥豫喧?
wxxdemac-mini:~ hncy-ios$ open ~/.bash_profile.pysave
The file /Users/hncy-ios/.bash_profile.pysave does not exist.
既然知道了系統(tǒng)默認(rèn)Python的安裝路徑,修改這個文件.bash_profile 如下:
# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH
PATH="/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/bin:${PATH}"
export PATH
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
# added by Anaconda3 5.1.0 installer
export PATH="/Users/hncy-ios/anaconda3/bin:$PATH"
export PATH="/System/Library/Frameworks/Python.framework/Versions/2.7/bin:$PATH"
重新打開終端
執(zhí)行python幢泼,如下:
wxxdemac-mini:~ hncy-ios$ python
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
為什么會這樣呢紧显?
原來終端的python命令響應(yīng),通過這個文件 .bash_profile 去尋找缕棵,而且是從下往上尋找孵班。
如果我將 .bash_profile 文件中的關(guān)于Python的路徑都注釋:
# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
#PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
#export PATH
PATH="/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/bin:${PATH}"
export PATH
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
# added by Anaconda3 5.1.0 installer
#export PATH="/Users/hncy-ios/anaconda3/bin:$PATH"
#export PATH="/System/Library/Frameworks/Python.framework/Versions/2.7/bin:$PATH"
重新打開終端
發(fā)現(xiàn):
wxxdemac-mini:~ hncy-ios$ python
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
wxxdemac-mini:~ hncy-ios$ python3
Python 3.6.1rc1 (default, Mar 4 2017, 22:58:58)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
哈哈哈,我奔潰/::)招驴。
echo $PATH
- 執(zhí)行echo $PATH篙程,可以看到當(dāng)前運行的所有環(huán)境變量。
- shell在搜索時先搜索PATH環(huán)境變量中的第一個目錄别厘,沒找到再接著搜索虱饿,如果找到則執(zhí)行它,不會再繼續(xù)搜索。
接著上面氮发,注釋 .bash_profile
文件中的所有關(guān)于Python
的路徑配置渴肉。終端執(zhí)行echo $PATH
,可以看到:
wxxdemac-mini:~ hncy-ios$ echo $PATH
/Users/hncy-ios/.rvm/gems/ruby-2.2.2/bin:/Users/hncy-ios/.rvm/gems/ruby-2.2.2@global/bin:/Users/hncy-ios/.rvm/rubies/ruby-2.2.2/bin:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
打印出來的是環(huán)境變量折柠,每個環(huán)境變量用冒號:
分開了宾娜,也就是有如下的環(huán)境變量:
/Users/hncy-ios/.rvm/gems/ruby-2.2.2/bin
/Users/hncy-ios/.rvm/gems/ruby-2.2.2@global/bin
/Users/hncy-ios/.rvm/rubies/ruby-2.2.2/bin
/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/bin
/usr/local/bin
/usr/bin:
/bin:/usr/sbin
/sbin
解開文件 .bash_profile 中關(guān)于anaconda3
的路徑的注釋:
# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
#PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
#export PATH
PATH="/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/bin:${PATH}"
export PATH
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
# added by Anaconda3 5.1.0 installer
export PATH="/Users/hncy-ios/anaconda3/bin:$PATH"
#export PATH="/System/Library/Frameworks/Python.framework/Versions/2.7/bin:$PATH"
再次打開終端運行echo $PATH
,發(fā)現(xiàn):
wxxdemac-mini:~ hncy-ios$ echo $PATH
/Users/hncy-ios/anaconda3/bin:/Users/hncy-ios/.rvm/gems/ruby-2.2.2/bin:/Users/hncy-ios/.rvm/gems/ruby-2.2.2@global/bin:/Users/hncy-ios/.rvm/rubies/ruby-2.2.2/bin:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/hncy-ios/.rvm/bin
很明顯扇售,多了這個環(huán)境變量:
/Users/hncy-ios/anaconda3/bin
查看當(dāng)前python
的環(huán)境變量:
wxxdemac-mini:~ hncy-ios$ which python
/Users/hncy-ios/anaconda3/bin/python
這可以看出:
-
anaconda3
的環(huán)境變量前塔,是通過修改.bash_profile
文件,達(dá)到動態(tài)添加到PATH
中承冰。 - 而Mac自帶版本號為
2.7
的python
環(huán)境华弓,和后來安裝的版本為3.6
的python
環(huán)境,各自的環(huán)境變量如下困乒,他們的環(huán)境變量是固定存在PATH
中寂屏,所以就算注釋掉.bash_profile
文件中的配置,終端仍然可以執(zhí)行娜搂。
wxxdemac-mini:~ hncy-ios$ which python
/usr/bin/python
wxxdemac-mini:~ hncy-ios$ which python3
/usr/local/bin/python3
也就是:
/usr/bin
/usr/local/bin
關(guān)于Mac下環(huán)境變量的配置
- 系統(tǒng)級別
/etc/profile
/etc/paths
- 用戶級別
~/.bash_profile
~/.bash_login
~/.profile
~/.bashrc