使用Sublime Text 3能以轻量级的环境写python脚本,运行python代码。为了更加方便地调用python脚本,通过在Sublime Text 3中绑定快捷键的方式,实现一键调用python交互运行环境,操作方式如下:
1、打开 Sublime text 3 安装 package control
在Tools目录下点击安装Package Control(最后一个)
2、安装 SublimeREPL
在Sublime text 3 中,通过快捷键 Ctrl+shift+p,键入install packages,在弹出的输入框里面,输入SublimeREPL。稍等安装后,即可看到Tools->SublimeREPL->Python菜单
3、绑定快捷键 F5(也可以改为其它快捷键)
打开菜单Preferences->Key Bindings-User,然后输入
{ "keys": ["f5"], "caption": "SublimeREPL:Python",
"command": "run_existing_window_command", "args":
{
"id": "repl_python_run",
"file": "config/Python/Main.sublime-menu"
}
},
这样在Sublime Text 3里面输入python脚本后,保存为py文件,然后只要按一下 F5 ,就能够一键执行python脚本了
由于本人安装的python有python 2.7和python 3.6两个版本,默认是python 2.7,所以刚才按F5快捷键后就默认跳到python 2.7的默认环境,如果要定义两个不同的快捷键,自由切换python 2.7和python 3.6,那可以这样做
打开目录 C:\Users\chinamobile\AppData\Roaming\Sublime Text 3\Packages\SublimeREPL\config\Python
把里面关于cmd python的改为cmd python36。(注:为区分python 2.7,本机的python 3.6命令为python36),修改后如下:
[
{
"id": "tools",
"children":
[{
"caption": "SublimeREPL",
"mnemonic": "R",
"id": "SublimeREPL",
"children":
[
{"caption": "Python",
"id": "Python",
"children":[
{"command": "repl_open",
"caption": "Python",
"id": "repl_python",
"mnemonic": "P",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["python36", "-i", "-u"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"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": ["python36", "-i", "-u", "-m", "pdb", "$file_basename"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"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": ["python36", "-u", "$file_basename"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"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": ["python36", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"],
"linux": ["python36", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"],
"windows": ["python36", "-u", "${packages}/SublimeREPL/config/Python/ipy_repl.py"]
},
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {
"PYTHONIOENCODING": "utf-8",
"SUBLIMEREPL_EDITOR": "$editor"
}
}
}
]}
]
}]
}
]
然后在菜单Preferences->Key Bindings-User中,再增加新的快捷键,如F6,修改后如下:
[
{ "keys": ["f5"], "caption": "SublimeREPL:Python",
"command": "run_existing_window_command", "args":
{
"id": "repl_python_run",
"file": "config/Python/Main.sublime-menu"
}
},
{ "keys": ["f6"], "caption": "SublimeREPL:Python",
"command": "run_existing_window_command", "args":
{
"id": "repl_python_run",
"file": "config/Python/Main-python36.sublime-menu"
}
},
]
这样就完成了,F5就一键切换到python 2.7环境,F6就一键切换到python 3.6环境,非常方便