美文网首页
Mac 下 终端执行python的 环境问题

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

作者: 上发条的树 | 来源:发表于2019-01-15 16:25 被阅读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

    • 执行echo $PATH,可以看到当前运行的所有环境变量。
    • shell在搜索时先搜索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
    

    这可以看出:

    • anaconda3的环境变量,是通过修改.bash_profile文件,达到动态添加到PATH中。
    • 而Mac自带版本号为2.7python环境,和后来安装的版本为3.6python环境,各自的环境变量如下,他们的环境变量是固定存在PATH中,所以就算注释掉.bash_profile文件中的配置,终端仍然可以执行。
    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

    �未完待续...

    相关文章

      网友评论

          本文标题:Mac 下 终端执行python的 环境问题

          本文链接:https://www.haomeiwen.com/subject/ccyksftx.html