mac python 升级 和 恢复

本文参考

  • 最新 python 下载地址
  • 安装后 运行 python --version 还是没变
是因为python
安装的路径是/Library/Frameworks/Python.framework
,而系统路径是System/Library/Frameworks/Python.framework
所以我们还要移动一下。

OSX 10.11引入了更为严格的安全机制,我们所提到的System/Library/Frameworks/Python.framework权限实际上比root还要高。。传说中的rootless,说实话比较坑。。不过我们还是有办法解决它:
1. 重启电脑
2. 按住Command+R直到出现苹果logo并读条,进入恢复模式
3. 打开恢复模式的终端输入命令:
bash
$ csrutil disable

如果还是没有权限,就在前面加上sudo
  • 更新脚本
#!/bin/bash
#python版号需要修改两个地方
#1. new_version

# sudo -i
#得到超级权限
new_version="3,5"

PYPATH=/System/Library/Frameworks/Python.framework/Versions/"$new_version"
#第1步移动新版python到mac默认目录下
echo "move.."
mv /Library/Frameworks/Python.framework/Versions/"$new_version" /System/Library/Frameworks/Python.framework/Versions/
#第2步改变用户目录的用户组
echo "chown.."
chown -R root:wheel ${PYPATH}
#第3步 删除原来2.7的链接
echo "del.."
rm /System/Library/Frameworks/Python.framework/Versions/Current
#第4步重新链接到最新版本的python
echo "ln.."
ln -s ${PYPATH} /System/Library/Frameworks/Python.framework/Versions/Current
#第5步删除旧的命令符号链接
echo "rm.."
rm /usr/bin/{pydoc,python,pythonw,python-config}
echo "ln bin.."
#第6步重新建立新的命令符号链接
ln -s ${PYPATH}/bin/pydoc"$new_version" /usr/bin/pydoc
ln -s ${PYPATH}/bin/python"$new_version" /usr/bin/python
ln -s ${PYPATH}/bin/pythonw"$new_version" /usr/bin/pythonw
ln -s ${PYPATH}/bin/python"$new_version"m-config /usr/bin/python-config

python_param_list=`cd /usr/local/bin && ls -al |grep "Python"|awk 'ORS=" " {print $9}'`
#第7步修复其他链接
for i in $python_param_list;do
echo "info: $i"
rm -f /usr/local/bin/${i}
ln -sv /System/Library/Frameworks/Python.framework/Versions/"$new_version"/bin/${i} /usr/local/bin/${i}
done

#第8步.环境变量要修改为最新的版本号
echo 'export PATH=/System/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}' >> ~/.bashrc

exit #退出超级权限
  • 恢复脚本
#!/bin/bash
#python版号需要修改两个地方
#1. new_version

# sudo -i
#得到超级权限
new_version="2.7"

PYPATH=/System/Library/Frameworks/Python.framework/Versions/"$new_version"
#第1步移动新版python到mac默认目录下
#第2步改变用户目录的用户组
echo "chown.."
chown -R root:wheel ${PYPATH}
#第3步 删除原来2.7的链接
echo "del.."
rm /System/Library/Frameworks/Python.framework/Versions/Current
#第4步重新链接到最新版本的python
echo "ln.."
ln -s ${PYPATH} /System/Library/Frameworks/Python.framework/Versions/Current
#第5步删除旧的命令符号链接
echo "rm.."
rm /usr/bin/{pydoc,python,pythonw,python-config}
echo "ln bin.."
#第6步重新建立新的命令符号链接
ln -s ${PYPATH}/bin/pydoc"$new_version" /usr/bin/pydoc
ln -s ${PYPATH}/bin/python"$new_version" /usr/bin/python
ln -s ${PYPATH}/bin/pythonw"$new_version" /usr/bin/pythonw
ln -s ${PYPATH}/bin/python"$new_version"m-config /usr/bin/python-config

python_param_list=`cd /usr/local/bin && ls -al |grep "Python"|awk 'ORS=" " {print $9}'`
#第7步修复其他链接
for i in $python_param_list;do
echo "info: $i"
rm -f /usr/local/bin/${i}
ln -sv /System/Library/Frameworks/Python.framework/Versions/"$new_version"/bin/${i} /usr/local/bin/${i}
done

#第8步.环境变量要修改为最新的版本号
echo 'export PATH=/System/Library/Frameworks/Python.framework/Versions/$new_version/bin:${PATH}' >> ~/.bashrc

exit #退出超级权限

你可能感兴趣的:(mac python 升级 和 恢复)