在使用Sublime Text3编写Python3代码时,发现自带的Python运行环境无法实现Python内容的交互(如执行input()方法时输入没有交互),因此需要安装SublimeREPL来实现对Python3的支持。
一、为Sublime Text3安装Package Control
去 https://packagecontrol.io/installation 找到安装Package Control代码片段(此处可能需要科学上网),或者直接复制如下代码。
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)
打开Sublime Text控制台( 快捷键:ctrl + ` 或者 view-->show console),粘贴代码并运行。
二、安装SublimeREPL
command + shift + p 打开命令提示框,输入Install Package(此处可能需要科学上网)。
然后输入SublimeREPL,点击安装。安装成功后可以看到Tools下面多了一个SublimeREPL。
三、配置Python3
Sublime Text --> Preferences --> Browse Packages,找到SublimeREPL中的config文件夹,在里面新建文件夹Python3,并将Python文件夹中的Default.sublime-commands 和 Main.sublime-menu拷贝到Python3中。
分别将Default.sublime-commands 和 Main.sublime-menu内的文本替换为如下内容。
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"
}
}
}
]}
]
}]
}
]
四、快捷键的设置
现在,如果需要使用Python3运行代码,可以去Tools-->SublimeREPL-->Python3 -->Python - RUN current file。
你还可以设置快捷键执行此动作:Sublime Text --> Preferences --> Key Bindings,添加如下代码(其中keys字段可以根据你的习惯自行指定):
[
{
"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执行代码前需对代码进行保存。
(完)