码迷,mamicode.com
首页 > 编程语言 > 详细

修改Python版本(for Mac OS X)

时间:2015-03-20 06:46:05      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

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
    • 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)

标签:

原文地址:http://www.cnblogs.com/lawrencechen/p/4352567.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!