[Python] VS Code 執行python程式與debug 設定教學
Published in
3 min readJan 3, 2019
今天來介紹如何在 VS Code 執行 python 程式,以及在執行 debug 模式時遇到的問題。
首先請先下載 python 套件,安裝方式跟上次 VS Code 中文化 一樣:
- 點擊Extension圖示(或是按下ctrl+shift+X)
- 在搜尋欄位輸入 Python 搜尋Python套件(提供自動補齊、語法檢查...等)
註. 若直接開啟.py檔, VS Code 也會跳出提醒問你要不要安裝Python套件
- 點擊 install 按鈕安裝 (安裝好記得點擊 reload 啟用)
- 開啟一個 .py 檔
- 滑鼠右鍵開啟選單並點擊 Run Python in Terminal => 成功執行 python
使用 debug 模式執行 python
- 按下鍵盤F5即可執行 debug 模式
- 如果出現警告視窗的話,表示得做一些設定,按下 Open launch.json,打開 launch.json ,待會會用到
- 左側選單切換到 Debug 頁籤(ctrl+shift+D)
- 打開下拉式選單選擇 python,發現沒有該選項
- 打開 launch.json 並加入以下 code 後儲存:
{ “name”: “Python: Current File”, “type”: “python”, “request”: “launch”, “program”: “${file}”,}
- 再次打開下拉式選單 (出現 python 了)
- 選擇 Python: Current File 並點擊綠色箭頭或直接按下F5=>成功啟用 debug 模式了
- 跟一般 debug 模式一樣,可下中斷點,Step Over, Step Into… 等
最後,如果你覺得我的文章還不錯,麻煩幫我按個拍手,謝謝 😀