1. 程式人生 > 實用技巧 >【學習筆記】VS Code的launch.json 的 Python和Chrome常用配置(MacOS)

【學習筆記】VS Code的launch.json 的 Python和Chrome常用配置(MacOS)

遇到的問題:

1、無法直接用VS Code呼叫Chrome來開啟HTML檔案

2、VS Code呼叫Chrome成功後,Python直譯器無法啟動除錯了

解決方法:

以下是我的 launch.json 檔案的配置資訊,供參考:

 1 {
 2     // 使用 IntelliSense 瞭解相關屬性。 
 3     // 懸停以檢視現有屬性的描述。
 4     // 欲瞭解更多資訊,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
 5     // ${workspaceRoot} 當前開啟的資料夾的絕對路徑+資料夾的名字
 6     // ${workspaceRootFolderName}   當前開啟的資料夾的名字
7 // ${file} 當前開啟正在編輯的檔名,包括絕對路徑,檔名,檔案字尾名 8 // ${fileBasename} 當前開啟的檔名+字尾名,不包括路徑 9 // ${fileBasenameNoExtension} 當前開啟的檔案的檔名,不包括路徑和字尾名 10 // ${fileDirname} 當前開啟的檔案所在的絕對路徑,不包括檔名 11 // ${fileExtname} 當前開啟的檔案的字尾名 12 // ${cwd} the task runner's current working directory on startup 13 // ${lineNumber} 當前開啟的檔案,游標所在的行數
14 // ${relativeFile} 從當前開啟的資料夾到當前開啟的檔案的路徑 15 "version": "0.2.0", 16 "configurations": [ 17 { 18 //HMTL的Chrome除錯 19 "name": "Launch Chrome", 20 "request": "launch", 21 "type": "pwa-chrome", 22 "url": "file://${file}", 23 "
webRoot": "${workspaceFolder}" 24 }, 25 { 26 //Python除錯 27 "name": "Python: 當前檔案", 28 "type": "python", 29 "request": "launch", 30 "program": "${file}", 31 "console": "integratedTerminal" 32 } 33 ] 34 }

注意事項:

1、需要呼叫Chrome來開啟HTML檔案時,我們是呼叫的本地檔案,所以,要用 file:// (本地檔案傳輸協議) 去找到我們在本地編輯的HTML檔案。

(網上很多給到的教程還是用的http://甚至沒有檔案傳輸協議,我都嘗試了,是無法找到對應的HTML檔案。)

2、VS Code中的launch.json檔案的各種變數,大家根據自己的需求去使用,我直接備註在了 launch.json 檔案裡面了,方便自己查閱。

 1     // ${workspaceRoot} 當前開啟的資料夾的絕對路徑+資料夾的名字
 2     // ${workspaceRootFolderName}   當前開啟的資料夾的名字
 3     // ${file} 當前開啟正在編輯的檔名,包括絕對路徑,檔名,檔案字尾名
 4     // ${fileBasename}  當前開啟的檔名+字尾名,不包括路徑
 5     // ${fileBasenameNoExtension} 當前開啟的檔案的檔名,不包括路徑和字尾名
 6     // ${fileDirname} 當前開啟的檔案所在的絕對路徑,不包括檔名
 7     // ${fileExtname} 當前開啟的檔案的字尾名
 8     // ${cwd} the task runner's current working directory on startup
 9     // ${lineNumber}  當前開啟的檔案,游標所在的行數
10     // ${relativeFile} 從當前開啟的資料夾到當前開啟的檔案的路徑