Mac 下 终端执行python的 环境问题

2019-01-15  本文已影响41人  上发条的树

参考

注意

对于 .bash_profile 文件的修改,终端需要重新打开,在执行命令,才能看到修改生效的结果。

实践

open ~/.bash_profile

打开这个文件.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"

从上往下看,依次是自己安装的3.6版本的Python

/Library/Frameworks/Python.framework/Versions/3.6

安装的anaconda3的Python版本

/Users/hncy-ios/anaconda3

此时没有看到系统自带的Python版本,终端执行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.
>>> 

终端执行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,环境配置覆盖了系统默认的Python版本,试着这样注释.bash_profile文件最后一行:

#export PATH="/Users/hncy-ios/anaconda3/bin:$PATH"

重新打开终端

终端执行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.
>>>

很棒,原来的环境回来了。

那么,系统默认安装路径是在哪里?
通过这样:

>>> 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
>>> 

可以看到,这个就是系统默认Python的安装路径

/System/Library/Frameworks/Python.framework/Versions/2.7

至于这个路径在哪里配置呢,文件 .bash_profile 中第二行注释是这么写的:

# The original version is saved in .bash_profile.pysave

尝试打开这个文件 .bash_profile.pysave,发现找不到这个文件,为啥?

wxxdemac-mini:~ hncy-ios$ open ~/.bash_profile.pysave
The file /Users/hncy-ios/.bash_profile.pysave does not exist.

既然知道了系统默认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"

重新打开终端

执行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命令响应,通过这个文件 .bash_profile 去寻找,而且是从下往上寻找。

如果我将 .bash_profile 文件中的关于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"

重新打开终端

发现:

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

接着上面,注释 .bash_profile 文件中的所有关于Python的路径配置。终端执行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

打印出来的是环境变量,每个环境变量用冒号:分开了,也就是有如下的环境变量:

/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 中关于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,发现:

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

很明显,多了这个环境变量:

/Users/hncy-ios/anaconda3/bin

查看当前python的环境变量:

wxxdemac-mini:~ hncy-ios$ which python
/Users/hncy-ios/anaconda3/bin/python

这可以看出:

wxxdemac-mini:~ hncy-ios$ which python
/usr/bin/python
wxxdemac-mini:~ hncy-ios$ which python3
/usr/local/bin/python3

也就是:

/usr/bin

/usr/local/bin


关于Mac下环境变量的配置

/etc/profile

/etc/paths

~/.bash_profile

~/.bash_login

~/.profile

~/.bashrc

�未完待续...

上一篇 下一篇

猜你喜欢

热点阅读