修改Python版本(for Mac OS X)

來源:互聯網
上載者:User

標籤:

Mac OS X 10.8及以後的版本都預裝了Python 2.7,但是在Mac上(Unix-like OS)上修改Python的版本並不如Windows方便。這篇文章的目標是要將Mac內建的Python 2.7升級為3.4.

 

本文部分:

  1. 下載並安裝新版Python
    • https://www.python.org/
  2. 將新版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/
  3. 修改Python串連檔案
    1. 修改Current串連檔案
      • sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.4 /System/Library/Frameworks/Python.framework/Versions/Current
    2. 刪除/usr/bin/下python相關串連檔案 
      • 如果這些檔案本身不是串連檔案,就要先刪除才能建立相應串連檔案。否則會提示File exists
      • /usr/bin/pydoc
      • /usr/bin/python
      • /usr/bin/pythonw
      • /usr/bin/python-config
    3. 重新建立/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
  4. 修改.bash_profile,指定使用者變數(需重啟Terminal才會生效)
    • PATH=/System/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}
    • export PATH
  5. 輸入python -V查看,應為修改後的版本。

 

有兩個地方值得注意:

  1. 不要刪除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.
  2. 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)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.