標籤:
Mac OS X 10.8及以後的版本都預裝了Python 2.7,但是在Mac上(Unix-like OS)上修改Python的版本並不如Windows方便。這篇文章的目標是要將Mac內建的Python 2.7升級為3.4.
本文部分:
- 下載並安裝新版Python
- 將新版Python複製到系統目錄下並修改檔案的owner
- sudo mv /Library/Frameworks/Python.framework/Versions/3.4/ /System/Library/Frameworks/Python.framework/Versions
- sudo chown -R root:wheel /System/Library/Frameworks/Python.framework/Versions/3.4/
- 修改Python串連檔案
- 修改Current串連檔案
- sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.4 /System/Library/Frameworks/Python.framework/Versions/Current
- 刪除/usr/bin/下python相關串連檔案
- 如果這些檔案本身不是串連檔案,就要先刪除才能建立相應串連檔案。否則會提示File exists
- /usr/bin/pydoc
- /usr/bin/python
- /usr/bin/pythonw
- /usr/bin/python-config
- 重新建立/usr/bin/下相應串連檔案
- sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.4/bin/pydoc3 /usr/bin/pydoc
- sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.4/bin/python3 /usr/bin/python
- sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.4/bin/python3 /usr/bin/pythonw
- sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4m-config /usr/bin/python-config
- 修改.bash_profile,指定使用者變數(需重啟Terminal才會生效)
- PATH=/System/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}
- export PATH
- 輸入python -V查看,應為修改後的版本。
有兩個地方值得注意:
- 不要刪除Mac內建的Python!因為可能Mac或第三方軟體會引用到。使用ls -l /System/Library/Frameworks/Python.framework/Versions 可以發現有2.3到2.7的各個版本的串連檔案也印證了這一點。網上搜出來的中文帖大概因為是複製粘貼的原因,全都先刪了內建的Python再說,不知是出於什麼心態。附帶Python.org的原文:
- The Apple-provided build of Python is installed in /System/Library/Frameworks/Python.framework and /usr/bin/python, respectively. You should never modify or delete these, as they are Apple-controlled and are used by Apple- or third-party software. Remember that if you choose to install a newer Python version from python.org, you will have two different but functional Python installations on your computer, so it will be important that your paths and usages are consistent with what you want to do.
- Python 2.7以後,將/usr/bin/python和/usr/bin/pythonw共同指向/System.../Versions/3.4/bin/python3即可,在我新安裝Python版本的bin/下是沒有pythonw這個命令。附帶原文:
- With older versions of Python, there is one Mac OS X quirk that you need to be aware of: programs that talk to the Aqua window manager (in other words, anything that has a GUI) need to be run in a special way. Use pythonw instead of python to start such scripts. With Python 3.4, you can use either python or pythonw.
由於本人接觸Linux和Python的時間較短,有錯誤或可以有更精簡的步驟歡迎提出來討論。_(:3」∠)_
修改Python版本(for Mac OS X)