如何选择mac python_macOS 上如何切换默认的 Python 版本?

难道大家不觉得在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文件夹中:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值