python版本升级 mac_Python:版本升级(Mac OS X)

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

正文部分:

将新版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/

修改Python连接文件修改Current连接文件

sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.4 /System/Library/Frameworks/Python.framework/Versions/Current

删除/usr/bin/下python相关连接文件/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」∠)_

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值