雖然網上教程一大堆,然而都不全面,遇到的各種坑的情況都沒寫。
一、安裝
前提是你安裝了Package Control,見Mac Sublime Text 3 配置Python環境及安裝插件
Preferences ->Package Control ->Package Control: Install Package,輸入SublimeREPL,回車安裝就OK了。
二、使用
1.交互式窗口
sublime運行Python沒法接收外部輸入的值,需要借助SublimeREPL插件開一個交互窗口。
使用方法:Tools->SublimeREPL->Python->Python-RUN current file
此處有坑:
比如你為了使代碼支持中文編碼,而在代碼開頭加入sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') #改變標准輸出的默認編碼
這時候運行交互窗口會報錯。或者其他一些報錯。解決辦法是設置SublimeREPL使用Python命令的版本。步驟如下:
Preferences ->Browse Packages 找到安裝包位置打開文件SublimeREPL/config/Python/Main.sublime-menu。找到如下代碼:
{"command": "repl_open",
"caption": "Python - RUN current file",
"id": "repl_python_run",
"mnemonic": "R",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": ["/Library/Frameworks/Python.framework/Versions/3.6/bin/python3", "-u", "$file_basename"],
"cwd": "$file_path",
"syntax": "Packages/Python/Python.tmLanguage",
"external_id": "python",
"extend_env": {"PYTHONIOENCODING": "utf-8"}
}
},
將cmd那行中的Python路徑改成你的新版Python路徑
2.逐行運行功能
a.Tools -> SublimeREPL -> Python -> Python , 打開一個 SublimeREPL調試頁面,別關。
b.回到代碼頁面,選中要執行的一行或多行語句, Tools -> SublimeREPL -> Eval REPL -> Lines,回到SublimeREPL的那個交互頁面,這個語句已經執行了,你可以打印你想查看的變量。
注意事項:

如上圖,我不能直接選中第13行執行,因為此時還不認識a和b。你可以第11、12、13,按順序一行一行的選中執行,或者這三行同時選中執行。
三、設置快捷鍵
要使用上面幾個功能太過於繁瑣,可以設置快捷鍵。
比如設置運行交互窗口快捷鍵"ctrl+b",打開一個 SublimeREPL頁面快捷鍵"ctrl+a",執行某行快捷鍵"ctrl+z"
Preferences -> Key Bindings 后在Default(Windows).sublime-keymap-User文件的方括號中輸入以下內容:
[ { "keys":["ctrl+b"], "caption": "SublimeREPL: Python - RUN current file", "command": "run_existing_window_command", "args": { "id": "repl_python_run", "file": "config/Python/Main.sublime-menu" } }, { "keys": ["ctrl+a"], "caption": "SublimeREPL: Python", "command": "run_existing_window_command", "args": { "id": "repl_python", "file": "config/Python/Main.sublime-menu" } }, { "keys": ["ctrl+z"], "command": "repl_transfer_current", "args": { "scope": "selection" } } ]
四、其他技巧
代碼窗口和插件窗口需要來回切換查看,非常不方便,可以設置同時顯示這幾個窗口,點擊View -> Layout -> Rows: 2
然后把插件的標簽頁脫下來,如下圖

