老 Mac OS 同時(shí)安裝 Python2.7 和 Python 3

老 Mac 同時(shí)安裝 Python2.7 和 Python 3

本文旨在比較舊 mac 系統(tǒng)上同時(shí)安裝 Python2.7 和 Python3。


首先參考http://www.itdecent.cn/p/51811fa24752 進(jìn)行了安裝,安裝過(guò)程中發(fā)現(xiàn)說(shuō)明文件有些過(guò)時(shí),因此在后面進(jìn)行安裝過(guò)程記錄,安裝完成后 python3 安裝模塊正常,安裝的模塊也在 site-packages中,但是導(dǎo)入模塊會(huì)出現(xiàn)錯(cuò)誤,可能是電腦的問(wèn)題。


由于需要在 python 3 中導(dǎo)入模塊,因此,最終采用的是以下方案:

python 2.7 使用 Mac 內(nèi)置的版本。

python 3.6 從https://www.python.org/downloads/release/python-364/ 下載安裝器安裝完成。

python 3.6 的安裝路徑為:/usr/loca/bin/python3。

安裝完成后,可以安裝模塊并導(dǎo)入。


下面的內(nèi)容記錄的是 Mac 系統(tǒng)參數(shù)和參考http://www.itdecent.cn/p/51811fa24752進(jìn)行安裝的過(guò)程。

Mac 系統(tǒng):


mac.png

參考資料安裝過(guò)程


參考:http://www.itdecent.cn/p/51811fa24752

步驟 內(nèi)容 結(jié)果 備注
1 安裝 Xcode 失敗 Xcode 是個(gè) IDE,用 Pycharm 代替
2 安裝套件管理工具 Homebrew 成功 安裝過(guò)程中遇到兩個(gè)問(wèn)題,已在文中列出
3 安裝 Python 成功 使用 brew install python 安裝 python3,使用brew install python@2 安裝 python2.7
4 設(shè)定路徑$PATH 成功
5 確認(rèn)安裝結(jié)果 失敗 可以運(yùn)行 python3 和 python2.7,但是 python3 安裝模塊后無(wú)法導(dǎo)入,造成無(wú)法使用。

Step 1: 安裝 Xcode


Xcode 是運(yùn)行在 Mac OS X 上的集成開(kāi)發(fā)工具( IDE ) ,可以在 App store 中搜索安裝,但是由于老 Mac 的最新版本是 10.11.16,不能滿(mǎn)足 Xcode 10.12.6 的要求,無(wú)法安裝,因此跳過(guò)此步驟,乖乖使用 pyCharm。

xcode.png

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


安裝 HomeBrew只需打開(kāi)Teminal,輸入以下命令:

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

但是,安裝過(guò)程中出現(xiàn)一連串的問(wèn)題:

安裝問(wèn)題 1 :無(wú)法連接 github ,導(dǎo)致無(wú)法下載

問(wèn)題是這樣的:

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

解決方案:

打開(kāi)一個(gè)新的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

屏幕上會(huì)提示輸入密碼(輸入密碼的時(shí)候不會(huì)有任何字符顯示,甚至*都不會(huì)顯示,輸完之后按回車(chē)),這是屏幕顯示的內(nèi)容就是hosts文件的內(nèi)容了。

按 i 進(jìn)入插入模式,換行寫(xiě)入以下內(nèi)容:

192.30.253.113 github.com

然后按esc退出插入模式,輸入 :wq然后按回車(chē)退出。

再次輸入以下命令:

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

即可實(shí)現(xiàn)下載。

問(wèn)題2:下載安裝后運(yùn)行brew doctor,顯示警告

警告內(nèi)容為:

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

按照提示,運(yùn)行:

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

沒(méi)有任何反應(yīng),但是再次運(yùn)行brew doctor,繼續(xù)顯示相同的警告內(nèi)容。這行命令的意思是將環(huán)境變量輸出到/.bash_profile中。于是先檢查/.bash_profile。輸入以下命令:

open -e .bash_profile

看到以下內(nèi)容:

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

似乎沒(méi)有問(wèn)題,運(yùn)行source .bash_profile,報(bào)以下錯(cuò)誤:

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

問(wèn)題出在第二行最后的雙引號(hào),復(fù)制第三行最后的雙引號(hào)替換第二行最后的雙引號(hào)即可。

再次運(yùn)行brew doctor,出現(xiàn)以下警告信息:

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,使用以下命令進(jìn)行安裝:

     brew install python3
    
    

安裝完成后,查看 /usr/local/Cellar,發(fā)現(xiàn)只有 python 文件夾,文件夾下有 3.6.4_4。
?

  1. 安裝python,使用以下命令進(jìn)行安裝:

    brew install python
    
    

結(jié)果顯示安裝成功,安裝完成后,文件夾下面還是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 ,應(yīng)該使用以下命令:

   brew install python@2

這時(shí)我們可以看到 /usr/local/Cellar 增加了 python@2文件夾。

step 4 設(shè)定路徑 $PATH


mac 內(nèi)置的 python 路徑為 /usr/bin,我們?cè)?step3 中安裝的 python2.7 和 python3.6 路徑為 /usr/local/bin。

在使用 terminal 啟動(dòng)程序時(shí),會(huì)安裝 /etc/paths 文件中的路徑搜索程序。因此,啟動(dòng)新安裝的程序,需要 /etc/paths 文件中 /usr/local/bin 在 /usr/bin 之前。

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

step5 確認(rèn)安裝結(jié)果


在 terminal 中運(yùn)行 which python,確認(rèn)輸出為:/usr/local/bin/python。

在 terminal 中運(yùn)行 which python3,確認(rèn)輸出為:/usr/loca/bin/python3。

進(jìn)一步,測(cè)試 python 安裝模塊命令 pip。這里我們使用 Pillow 模塊進(jìn)行測(cè)試。

運(yùn)行以下命令為 python 2.7 安裝 Pillow:

pip install pillow

安裝完成后測(cè)試是否可用,在 terminal 中輸入 python,然后輸入以下指令:

from PIL import Images

如果沒(méi)有報(bào)錯(cuò),則 python 2.7 的 pip 正常運(yùn)行。

然后運(yùn)行以下命令為 Python 3.6 安裝 Pillow:

pip3 install pillow

安裝完成后在 terminal 中輸入 python3 進(jìn)入 shell ,然后輸入以下指令:

from PIL import Images

結(jié)果報(bào)錯(cuò):ModuleNotFoundError,安裝其他模塊也會(huì)出現(xiàn)上述錯(cuò)誤。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容