設定Visual Studio Code Python 開發環境建置作業

2 月 22, 2018 | | 0 條留言

Python的擴充功能

只要你用Visual Studio Code打開一個Python.py檔案,VSCode都會貼心的提示你要進行安裝Python的擴充功能(如下圖所示)。

提示你要進行安裝Python的擴充功能

Python的擴充功能

當然安裝完成擴充功能,還需要寫完程式時的建置作業,總不能在每次寫完一行程式就用cmd的方式去run你的結果吧!

VSCode Python建置作業

當然VSCode也提供了完善的建置作業。只需要簡單的設定,之後就需要使用快捷鍵或按鈕就可以完成。

選單工作列上選擇 工作 > 工作設定 ,如下開啟一個tasks.json檔案。

vscode tasks

tasks.json 是VScode的執行建置的設定配置檔案,他可以設定許多不同的建置需求,每一個tasks可以設定不同的建置需求。

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Run tests",
            "type": "shell",
            "command": "script test"
        }
        ,
        {
            "label": "Run tests2",
            "type": "shell",
            "command": "script test"
        }
    ]
}

Python的建置設定如以下使用的是shell command,設定如下。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
            {
                "taskName": "Run Python Code",
                "type": "shell",
                "command": "python",
                "args": [
                    "'NULL'"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "presentation": {
                    "echo": true,
                    "reveal": "always",
                    "focus": true,
                    "panel": "shared"
                }
            }
        ]
}

Ctrl + Shift + B (OS X:CMD + SHIFT + B) 來執行程式,結果會輸出於下方法。

vscode tasks

vscode tasks