美文网首页
老 Mac OS 同时安装 Python2.7 和 Python

老 Mac OS 同时安装 Python2.7 和 Python

作者: 学以致用123 | 来源:发表于2018-03-12 11:06 被阅读0次

    老 Mac 同时安装 Python2.7 和 Python 3

    本文旨在比较旧 mac 系统上同时安装 Python2.7 和 Python3。


    首先参考https://www.jianshu.com/p/51811fa24752 进行了安装,安装过程中发现说明文件有些过时,因此在后面进行安装过程记录,安装完成后 python3 安装模块正常,安装的模块也在 site-packages中,但是导入模块会出现错误,可能是电脑的问题。


    由于需要在 python 3 中导入模块,因此,最终采用的是以下方案:

    python 2.7 使用 Mac 内置的版本。

    python 3.6 从https://www.python.org/downloads/release/python-364/ 下载安装器安装完成。

    python 3.6 的安装路径为:/usr/loca/bin/python3。

    安装完成后,可以安装模块并导入。


    下面的内容记录的是 Mac 系统参数和参考https://www.jianshu.com/p/51811fa24752进行安装的过程。

    Mac 系统:


    mac.png

    参考资料安装过程


    参考:https://www.jianshu.com/p/51811fa24752

    步骤 内容 结果 备注
    1 安装 Xcode 失败 Xcode 是个 IDE,用 Pycharm 代替
    2 安装套件管理工具 Homebrew 成功 安装过程中遇到两个问题,已在文中列出
    3 安装 Python 成功 使用 brew install python 安装 python3,使用brew install python@2 安装 python2.7
    4 设定路径$PATH 成功
    5 确认安装结果 失败 可以运行 python3 和 python2.7,但是 python3 安装模块后无法导入,造成无法使用。

    Step 1: 安装 Xcode


    Xcode 是运行在 Mac OS X 上的集成开发工具( IDE ) ,可以在 App store 中搜索安装,但是由于老 Mac 的最新版本是 10.11.16,不能满足 Xcode 10.12.6 的要求,无法安装,因此跳过此步骤,乖乖使用 pyCharm。

    xcode.png

    step 2: 安装套件管理工具 Homebrew


    安装 HomeBrew只需打开Teminal,输入以下命令:

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    

    但是,安装过程中出现一连串的问题:

    安装问题 1 :无法连接 github ,导致无法下载

    问题是这样的:

    Fatal:unable to access 'https://github.com/Homebrew/brew':Failed to connect to github.com port 443:Operation time out</pre>
    

    解决方案:

    打开一个新的Terminal,输入:ping github.com

    bogon:~ apple$ ping github.com
    PING github.com (192.30.255.112): 56 data bytes
    64 bytes from 192.30.255.112: icmp_seq=1 ttl=48 time=370.066 ms
    64 bytes from 192.30.255.112: icmp_seq=2 ttl=48 time=301.871 ms
    64 bytes from 192.30.255.112: icmp_seq=4 ttl=48 time=299.229 ms
    64 bytes from 192.30.255.112: icmp_seq=5 ttl=48 time=300.999 ms
    

    按ctrl+C停止,然后输入以下命令:

    sudo vi /etc/hosts
    

    屏幕上会提示输入密码(输入密码的时候不会有任何字符显示,甚至*都不会显示,输完之后按回车),这是屏幕显示的内容就是hosts文件的内容了。

    按 i 进入插入模式,换行写入以下内容:

    192.30.253.113 github.com
    

    然后按esc退出插入模式,输入 :wq然后按回车退出。

    再次输入以下命令:

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    

    即可实现下载。

    问题2:下载安装后运行brew doctor,显示警告

    警告内容为:

    Homebrew's sbin was not found in your PATH but you have installed
    formulae that put executables in /usr/local/sbin.
    Consider setting the PATH for example like so
     echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.bash_profile
    

    按照提示,运行:

    >> echo 'export PATH="/usr/local/sbin:$PATH"' 
    >> ~/.bash_profile
    

    没有任何反应,但是再次运行brew doctor,继续显示相同的警告内容。这行命令的意思是将环境变量输出到/.bash_profile中。于是先检查/.bash_profile。输入以下命令:

    open -e .bash_profile
    

    看到以下内容:

    export PATH=/Library/PostgreSQL/9.5/bin:$PATH
    export DYLD_LIBRARY_PATH="/Library/PostgreSQL/9.5/bin“
    export PATH="/usr/local/sbin:$PATH"
    

    似乎没有问题,运行source .bash_profile,报以下错误:

    -bash: .bash_profile: line 3: unexpected EOF while looking for matching `"'
    -bash: .bash_profile: line 4: syntax error: unexpected end of file
    

    问题出在第二行最后的双引号,复制第三行最后的双引号替换第二行最后的双引号即可。

    再次运行brew doctor,出现以下警告信息:

    Please note that these warnings are just used to help the Homebrew maintainers
    with debugging if you file an issue. If everything you use Homebrew for is
    working fine: please don't worry and just ignore them. Thanks!
    ​
    Warning: Setting DYLD_* vars can break dynamic linking.
    Set variables:
     DYLD_LIBRARY_PATH: /Library/PostgreSQL/9.5/bin
    

    step 3 安装 python


    1. 安装 python3,使用以下命令进行安装:

       brew install python3
      
      

    安装完成后,查看 /usr/local/Cellar,发现只有 python 文件夹,文件夹下有 3.6.4_4。

    1. 安装python,使用以下命令进行安装:

      brew install python
      
      

    结果显示安装成功,安装完成后,文件夹下面还是3.6.4_4。

    使用以下命令查找安装源文件:

    brew info python
    

    可以看到以下输出:

        python: stable 3.6.4 (bottled), devel 3.7.0b2, HEAD
        Interpreted, interactive, object-oriented programming language
        https://www.python.org/
        /usr/local/Cellar/python/3.6.4_3 (3,615 files, 56.3MB)
         Poured from bottle on 2018-03-09 at 18:08:04
        /usr/local/Cellar/python/3.6.4_4 (4,615 files, 97.4MB)
         Poured from bottle on 2018-03-11 at 15:02:08
        From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/python.rb
        ==> Dependencies
        Build: pkg-config ✔, sphinx-doc ✘
        Required: gdbm ✔, openssl ✔, readline ✔, sqlite ✔, xz ✔
        Optional: tcl-tk ✘
        ==> Options
        --with-tcl-tk
         Use Homebrew's Tk instead of macOS Tk (has optional Cocoa and threads support)
        --devel
         Install development version 3.7.0b2
        --HEAD
         Install HEAD version
        ==> Caveats
        Python has been installed as
         /usr/local/bin/python3
        ​
        Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
        `python3`, `python3-config`, `pip3` etc., respectively, have been installed into
         /usr/local/opt/python/libexec/bin
        ​
        If you need Homebrew's Python 2.7 run
         brew install python@2
        ​
        Pip, setuptools, and wheel have been installed. To update them run
         pip3 install --upgrade pip setuptools wheel
        ​
        You can install Python packages with
         pip3 install <package>
        They will install into the site-package directory
         /usr/local/lib/python3.6/site-packages
        ​
        See: https://docs.brew.sh/Homebrew-and-Python</pre>
    

    我们可以看到,如果需要安装 python2.7 ,应该使用以下命令:

       brew install python@2
    

    这时我们可以看到 /usr/local/Cellar 增加了 python@2文件夹。

    step 4 设定路径 $PATH


    mac 内置的 python 路径为 /usr/bin,我们在 step3 中安装的 python2.7 和 python3.6 路径为 /usr/local/bin。

    在使用 terminal 启动程序时,会安装 /etc/paths 文件中的路径搜索程序。因此,启动新安装的程序,需要 /etc/paths 文件中 /usr/local/bin 在 /usr/bin 之前。

    使用 sudo vi /etc/paths 或者 sudo emacs /etc/paths 更改 /etc/path 文件中路径顺序,然后重启 terminal 即可。

    step5 确认安装结果


    在 terminal 中运行 which python,确认输出为:/usr/local/bin/python。

    在 terminal 中运行 which python3,确认输出为:/usr/loca/bin/python3。

    进一步,测试 python 安装模块命令 pip。这里我们使用 Pillow 模块进行测试。

    运行以下命令为 python 2.7 安装 Pillow:

    pip install pillow
    

    安装完成后测试是否可用,在 terminal 中输入 python,然后输入以下指令:

    from PIL import Images
    

    如果没有报错,则 python 2.7 的 pip 正常运行。

    然后运行以下命令为 Python 3.6 安装 Pillow:

    pip3 install pillow
    

    安装完成后在 terminal 中输入 python3 进入 shell ,然后输入以下指令:

    from PIL import Images
    

    结果报错:ModuleNotFoundError,安装其他模块也会出现上述错误。

    相关文章

      网友评论

          本文标题:老 Mac OS 同时安装 Python2.7 和 Python

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