
在使用Sublime Text3編寫(xiě)Python3代碼時(shí),發(fā)現(xiàn)自帶的Python運(yùn)行環(huán)境無(wú)法實(shí)現(xiàn)Python內(nèi)容的交互(如執(zhí)行input()方法時(shí)輸入沒(méi)有交互),因此需要安裝SublimeREPL來(lái)實(shí)現(xiàn)對(duì)Python3的支持。
一、為Sublime Text3安裝Package Control
去 https://packagecontrol.io/installation 找到安裝Package Control代碼片段(此處可能需要科學(xué)上網(wǎng)),或者直接復(fù)制如下代碼。
import urllib.request,os,hashlib; h = '6f4c264a24d933ce70df5dedcf1dcaee' + 'ebe013ee18cced0ef93d5f746d80ef60'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)
打開(kāi)Sublime Text控制臺(tái)( 快捷鍵:ctrl + ` 或者 view-->show console),粘貼代碼并運(yùn)行。
二、安裝SublimeREPL
command + shift + p 打開(kāi)命令提示框,輸入Install Package(此處可能需要科學(xué)上網(wǎng))。

然后輸入SublimeREPL,點(diǎn)擊安裝。安裝成功后可以看到Tools下面多了一個(gè)SublimeREPL。

三、配置Python3
Sublime Text --> Preferences --> Browse Packages,找到SublimeREPL中的config文件夾,在里面新建文件夾Python3,并將Python文件夾中的Default.sublime-commands 和 Main.sublime-menu拷貝到Python3中。

分別將Default.sublime-commands 和 Main.sublime-menu內(nèi)的文本替換為如下內(nèi)容。
Default.sublime-commands:
[
{
"caption": "SublimeREPL: Python3",
"command": "run_existing_window_command", "args":
{
"id": "repl_python3",
"file": "config/Python3/Main.sublime-menu"
}
},
{
"caption": "SublimeREPL: Python - PDB current file",
"command": "run_existing_window_command", "args":
{
"id": "repl_python_pdb",
"file": "config/Python3/Main.sublime-menu"
}
},
{
"caption": "SublimeREPL: Python3 - RUN current file",
"command": "run_existing_window_command", "args":
{
"id": "repl_python3_run",
"file": "config/Python3/Main.sublime-menu"
}
},
{
"command": "python_virtualenv_repl",
"caption": "SublimeREPL: Python - virtualenv"
},
{
"caption": "SublimeREPL: Python - IPython",
"command": "run_existing_window_command", "args":
{
"id": "repl_python_ipython",
"file": "config/Python3/Main.sublime-menu"
}
}
]
Main.sublime-menu:
[
{
"id": "tools",
"children":
[{
"caption": "SublimeREPL",
"mnemonic": "R",
"id": "SublimeREPL",
"children":
[
{"caption": "Python3",
"id": "Python3",
"children":[
{"command": "repl_open",
"caption": "Python3",
"id": "repl_python3",
"mnemonic": "P",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python3", "-i", "-u"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python3",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
},
{"command": "python_virtualenv_repl",
"id": "python_virtualenv_repl",
"caption": "Python - virtualenv"},
{"command": "repl_open",
"caption": "Python - PDB current file",
"id": "repl_python_pdb",
"mnemonic": "D",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python3", "-i", "-u", "-m", "pdb", "$file_basename"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python3",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
},
{"command": "repl_open",
"caption": "Python - RUN current file",
"id": "repl_python_run",
"mnemonic": "R",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python3", "-u", "$file_basename"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python3",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
},
{"command": "repl_open",
"caption": "Python - IPython",
"id": "repl_python_ipython",
"mnemonic": "I",
"args": {
"type": "subprocess",
"encoding": "utf8",
"autocomplete_server": true,
"cmd": {
"osx": ["python3", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"],
"linux": ["python3", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"],
"windows": ["python3", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"]
},
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python3",
"extend_env": {
"PYTHONIOENCODING": "utf-8",
"SUBLIMEREPL_EDITOR": "$editor"
}
}
}
]}
]
}]
}
]
四、快捷鍵的設(shè)置
現(xiàn)在,如果需要使用Python3運(yùn)行代碼,可以去Tools-->SublimeREPL-->Python3 -->Python - RUN current file。

你還可以設(shè)置快捷鍵執(zhí)行此動(dòng)作:Sublime Text --> Preferences --> Key Bindings,添加如下代碼(其中keys字段可以根據(jù)你的習(xí)慣自行指定):
[
{
"keys":["command+shift+b"],
"caption": "SublimeREPL: Python - RUN current file",
"command": "run_existing_window_command", "args": {"id": "repl_python_run",
"file": "config/Python3/Main.sublime-menu"}
}
]
PS. 和Sublime Text3自帶的解釋器不同,使用SublimeREPL執(zhí)行代碼前需對(duì)代碼進(jìn)行保存。
(完)