Python:版本升级(Mac OS X)

Mac OS X 10.8及以后的版本都预装了Python 2.7,但是在Mac上(Unix-like OS)上修改Python的版本并不如Windows方便。这篇文章的目标是要将Mac自带的Python 2.7升级为3.4.

 

正文部分:

  1. 下载并安装新版Python
  2. 将新版Python复制到系统目录下并修改文件的owner
    • 注意这里使用cp而不是mv,否则升级完成后某些包还是会引用到安装目录:/Library/Frameworks/...的lib文件夹,例如无法import readline,导致方向键失效。理论上将指向全部改为/System/Library/...下即可,如果你知道怎么修改,请务必留言。
    • sudo cp -r /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相关连接文件 
      • /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」∠)_

 

posted @ 2015-03-20 02:18  SCVTheDefect  阅读(1514)  评论(0编辑  收藏  举报