修復(fù)macOS 10.12 的 Python2.7環(huán)境(為了讓Xcode能打包)

把系統(tǒng)的python(路徑/System/Library/Frameworks/Python.framework/Versions)從2.7升級(jí)到了3.6,然后Xcode就GG了。

打包的時(shí)候報(bào)錯(cuò)

{
description = "Failed to verify bitcode in Frameworks/libswiftAssetsLibrary.dylib:\nTraceback (most recent call last):\n  File \"/Applications/Xcode.app/Contents/Developer/usr/bin/bitcode-build-tool\", line 7, in <module>\n    from bitcode_build_tool import bitcode_build_tool_main, BitcodeBuildFailure\n  File \"/Applications/Xcode.app/Contents/Developer/usr/bin/../lib/bitcode_build_tool/__init__.py\", line 1, in <module>\n    from buildenv import BuildEnvironment, BitcodeBuildFailure\nModuleNotFoundError: No module named 'buildenv'\n";
info = {
    };
    level = ERROR;
   type = "malformed-payload";
}

看到里面的__init__.py文件出錯(cuò),想到剛升級(jí)了python,就知道估計(jì)這里出了問題了。原因可能是后來(lái)定位問題時(shí)看到的一句話:

MAC OS X EI Capitan 系統(tǒng)的 python 從 2.7 升級(jí)到 3 ,如果是 IOS 開發(fā)者請(qǐng)不要直接把 2.7 干掉,因?yàn)?xcode 只支持 2.7 的 python

于是開始修復(fù)python到python2.7.

開始修復(fù)

  1. 用brew下載安裝2.7(如果你不像我把python2.7的文件夾都給刪了,那么可以跳過)
brew update && brew reinstall python    
brew unlink python && brew link python

如果這個(gè)時(shí)候/System/Library/Frameworks/Python.framework/Versions/里面沒有2.7的文件夾,復(fù)制/usr/local/Cellar/python/2.7.X進(jìn)去。

注意:這里要打開SIP才可以操作,不過如果你已經(jīng)搞壞了python2.7 =。=,估計(jì)SIP也是開的。

  1. 重新建立軟鏈接。

下面是當(dāng)時(shí)升級(jí)到3.6的操作,嘗試把3.6改回2.7,結(jié)果可行。

sudo chown -R root:wheel /System/Library/Frameworks/Python.framework/Versions/2.7
sudo rm /System/Library/Frameworks/Python.framework/Versions/Current
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7 /System/Library/Frameworks/Python.framework/Versions/Current
sudo rm /usr/bin/pydoc
sudo rm /usr/bin/pythonbrew unlink python && brew link python
sudo rm /usr/bin/pythonw
sudo rm /usr/bin/python-config
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/pydoc2.7 /usr/bin/pydoc
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /usr/bin/python
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7 /usr/bin/pythonw
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7m-config /usr/bin/python-config
  1. 重裝一下xcode 的 command-tool
sudo xcode-select --install

好了,到這里應(yīng)該就能搞定了。

熟悉的python又回來(lái)了。

image.png

補(bǔ)充:.bash_profile文件

如果終端用的bash,把我的.bash_profile文件內(nèi)容是這些,亂糟糟的一堆。這東西不熟,貼出來(lái)做個(gè)參考

source ~/.profile

[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
if which swiftenv > /dev/null; then eval "$(swiftenv init -)"; fi

PATH="/System/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

如果要默認(rèn)用python3,那么以后用python3和pip3就好,別把系統(tǒng)的干掉了。

當(dāng)然,也可以用alias寫入.bash_profile

alias python="/System/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6"
最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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