难道大家不觉得在terminal中输入python filename.py的方式很麻烦吗?个人觉得在sublime中command+b运行python更方便,而且通过自己写build system来随意切换python版本。
1.mac中python的安装
mac系统会自带python,一般是python2.6或python2.7。但是你可能需要用到python3.x,所以需要自己在mac系统中安装python。推荐采用homebrew安装python。
第一步安装homebrew,在terminal中输入命令:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
使用homebrew安装python3,在terminal中输入:
brew install python3
会安装最新版的python
2.使用sublime编译py文件
sublime自带有python的编译系统,在sublime中的Tools-build system中:
在上图中选择python,在py文件中按command+b就会用python编译运行。选最上方的automatic,sublime会根据文件类型选择编译器。
3.sublime text中可以自己写build system,首先看看效果:
可以看到里面有Python和Python-3.6两个编译选项。下面介绍怎么编写这个新的Python-3.6的编译选项。参考Build Systems - Basics。
首先Tool - build system - New build system,这个会打开一个新的文档,里面有这样的代码:
{
"shell_cmd": "make"
}
删掉上面的代码,然后将下面的代码粘贴到文档里:
{
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
下面解释这段代码的含义:
"cmd": ["python", "-u", "$file"],
这段就相当于在terminal中输入:python -u file
要使用python3.6编译文件,需要将上面的python替换为python3.6所在的路径,在terminal中输入:
which python3.6
得到路径:/usr/local/bin/python3.6。替换之后得到:
{
"cmd": ["/usr/local/bin/python3.6", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
保存这个文件,命名Python-3.6。之后在build system中就可以找到了。写好的文件在sublime packages文件夹中: