Mac python2.7 升级 python3.6

下载安装 python3.
下载链接:https://www.python.org/downloads/
python3安装后的位置在:

/Library/Frameworks/Python.framework/Versions/3.6

系统的 python 位置在:

/System/Library/Frameworks/Python.framework/Versions

查看 python 版本:

localhost:~ let$ python --version
Python 2.7.10
localhost:~ let$ python3 --version
Python 3.6.3

尝试移动位置:

localhost:~ let$ sudo mv /Library/Frameworks/Python.framework/Versions/3.6 /System/Library/Frameworks/Python.framework/Versions
Password:

提示语句如下

mv: rename /Library/Frameworks/Python.framework/Versions/3.6 to /System/Library/Frameworks/Python.framework/Versions/3.6: Operation not permitted

mac 当前版为:macOS High Sierra 10.13.2,苹果在 macOS 10.12 及以后加入了一个叫 SIP 的安全模式,可能会出现权限问题,需要先进行关闭:

  • 重启 mac
  • 重启时候按住 command+R
  • 进入恢复模式后,打开“实用工具-->终端”
  • 在终端中输入“csrutil disable”
  • 重启 mac


    image.png

升级步骤

1.移动位置

终端输入:

sudo mv /Library/Frameworks/Python.framework/Versions/3.6 /System/Library/Frameworks/Python.framework/Versions
Password:

会发现位置已经改变


image.png

2.改变 Python 安装目录的用户组

终端输入:

sudo chown -R root:wheel /System/Library/Frameworks/Python.framework/Versions/3.6

3.修改 Python 当前安装目录的快捷方式

在 /System/Library/Frameworks/Python.framework/Versions/ 目录下有一个 Current,这是一个目录符号链接,指向当前的 Python 版本。原来指向 2.7 的,现在指向我们安装的最新的。所以应先删除 Current,然后重新建立 Current 符号链接

  • 删除链接:

sudo rm /System/Library/Frameworks/Python.framework/Versions/Current

  • 建立连接:

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

4.删除旧的命令符号链接

在 /usr/bin 目录下有 4 个 Python 命令的符号链接,使用下面的命令先删除,如果没有的话可以不用管(分别输入):

sudo rm /usr/bin/pydoc
sudo rm /usr/bin/python
sudo rm /usr/bin/pythonw
sudo rm /usr/bin/python-config

5.重新建立新的命令符号链接

将上一部删除的符号链接重新使用下面命令建立,如果没有可以不管,将它们全部指向 Python 3.6(分别输入):

sudo ln -s System/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /usr/bin/python
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.6/bin/pythonw3.6 /usr/bin/pythonw
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6m-config /usr/bin/python-config

6. 更新 /root/.bash_profile 文件中的路径

首先打开.bash_profile

vim ~/.bash_profile

配置 python3

# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/System/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH 

7.查看 python 版本

localhost:~ let$ python --version
Python 3.6.3

8.打开SIP 的安全模式

  • 重启 mac
  • 重启时候按住 command+R
  • 进入恢复模式后,打开“实用工具-->终端”
  • 在终端中输入“csrutil enable”
  • 重启 mac

回退

Mac系统自带python2.7,鉴于python2 运用广泛、支持第三方框架,可以设置python2.7和python3.X两个版本共存,如果你升级了,可以按照步骤把设置3.X 的地方都设置回2.7.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。