1. 程式人生 > >vscode 配置 python3開發環境

vscode 配置 python3開發環境

vscode來寫python,配置靈活,介面美觀,是個非常好的選擇。我這裡是在ubuntu系統下配置vscode的python3開發環境,當然也可以參照本文在其它作業系統下配置vscode的python開發環境。

1 安裝外掛

  1. python
    python
    這個是vscode提供的python 官方外掛,提供了python程式碼的除錯,自動補全,程式碼格式化等功能
  2. vscode-icons
    vscode-icons
    這個也是vscode官方提供的外掛,作用是給vscode編輯的檔案增加圖示。
  3. Path Intellisense
    Path Intellisense
    這個外掛的作用是當代碼中讀入檔名或者檔案路徑時,提供檔名或者檔案路徑的自動補全
  4. topper
    topper
    這個外掛的作用是在.pyw檔案的開頭新增一些說明header
  5. Guides
    Guides
    這個外掛的作用是增加.py 中的指示線,用這個外掛能讓程式碼的層次結構更加清晰。
  6. Bracket Pair Colorizer
    Bracket Pair Colorizer
    這個外掛的作用是給程式碼中的括號增加顏色,同一對括號是相同的顏色,尤其是在括號中還包著括號的時候,看起來更加的清晰。

2 配置

可以在 這裡下載我的配置檔案,直接放在自己的python工作空間中。

2.1 建立Python資料夾

vscode 是基於資料夾的編輯器,我們可以首先建立一個資料夾叫做PYTHON,作為我們的Python程式設計工作空間,只要一次配置好了這個工作空間,以後這個工作空間的配置就會對它之下的所有的.py

檔案都起作用。

開啟vscode,點選左上角檔案 —> 開啟資料夾,然後開啟剛剛建立的PYTHON 資料夾。
然後我們點選PYTHON 資料夾右邊的新增檔案按鈕:
新增檔案
新增一個.py 檔案,名字叫做hellovscode.py .
hellovscode

2.2 配置launch.json 檔案

點選選單欄除錯 —> 開啟配置,就會彈出一個選擇框,我們在這裡要選擇Python,然後就打開了launch.json 檔案:
launch檔案
我們看到的launch.json 檔案中的內容如上圖所示。同時我們還發現,在python工作區PYTHON下面還多了一個資料夾.vscode, 而且launch.json

就在這個資料夾中。
launch.json 檔案的配置如下:

"configurations": [] z中,對於第一個{ }內的內容修改如下:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python3",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "/usr/bin/python3", //python3的安裝路徑
            "program": "${file}",
            "cwd": "${workspaceFolder}",
            "env": {},
            "envFile": "${workspaceFolder}/.env",
            "debugOptions": [
                "RedirectOutput"
            ]
        }        
    ]
}

後面幾個{ }中的內容修改如下:

        {
            "name": "Python: Terminal (integrated)",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "/usr/bin/python3",
            "program": "${file}",
            "cwd": "",
            "console": "integratedTerminal",
            "env": {},
            "envFile": "${workspaceFolder}/.env",
            "debugOptions": []
        },
        {
            "name": "Python: Terminal (external)",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "/usr/bin/python3",
            "program": "${file}",
            "cwd": "",
            "console": "externalTerminal",
            "env": {},
            "envFile": "${workspaceFolder}/.env",
            "debugOptions": []
        },
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "pythonPath": "/usr/bin/python3",
            "program": "${workspaceFolder}/manage.py",
            "cwd": "${workspaceFolder}",
            "args": [
                "runserver",
                "--noreload",
                "--nothreading"
            ],
            "env": {},
            "envFile": "${workspaceFolder}/.env",
            "debugOptions": [
                "RedirectOutput",
                "Django"
            ]
        },

其它地方都不用修改。

2.3 配置tasks.json 檔案

點選選單欄任務 —> 配置任務,就會彈出一個選擇框,我們在這裡要選擇使用模板建立tasks.json檔案,然後又彈出一個選擇框,這裡選擇Others,就打開了tasks.json 檔案:
tasks.json檔案
tasks.json 檔案的配置如下:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "python3",
            "type": "shell",
            "command": "/usr/bin/python3",
            "args": ["${file}"]
        }
    ]
}

2.4 使用者設定

點選選單欄檔案 —> 首選項—> 設定,然後開啟使用者設定
這裡寫圖片描述

使用者設定如下:

{
    "git.ignoreLegacyWarning": true,
    "workbench.iconTheme": "vscode-icons", //啟用vscode圖示
    "python.pythonPath": "/usr/bin/python3", // python3路徑
    "editor.lineHeight": 26, // 編輯器中的行高
    "editor.fontSize": 18, // 編輯器中的字型
    "editor.wordWrap": "on",
    "editor.formatOnSave": true, //編輯器自動儲存
    "python.linting.flake8Enabled": true, //啟用flake8,首先需要pip3 install falke8
    "python.formatting.provider": "yapf", ///啟用yapf,首先需要pip3 install yapf
    "editor.renderIndentGuides": false,
    "path-intellisense.autoSlashAfterDirectory": true,
    "path-intellisense.extensionOnImport": true,
    "workbench.colorTheme": "Monokai", // 配色方案
    "python.linting.pylintArgs": [
        "--load-plugins",
        "pylint_django",
        "--disable-msg=C0111"
    ],// 忽略的警告資訊
    // 下面是topper的插入header配置
    "topper.customTemplateParameters": [
        {
            "personalProfile": {
                "author": "你的名字",
                "website": "bulbasaur.github.bitbucket.yababbdadado.com",
                "copyright": "None \n None",
                "license": "None",
                "email": "你的郵箱"
            }
        },
        {
            "officeProfile": {
                "author": "John Doe",
                "department": "Product Development",
                "email": "[email protected]"
            }
        }
    ],
    "topper.headerTemplates": [
        {
            "defaultCStyled": {
                "headerBegin": "/**",
                "headerPrefix": "*",
                "headerEnd": "*/",
                "template": [
                    "${headerBegin}",
                    "${headerPrefix} ${fileName}",
                    "${headerPrefix} @author ${author}",
                    "${headerPrefix} @description ${description}",
                    "${headerPrefix} @created ${createdDate}",
                    "${headerPrefix} @copyright ${copyright}",
                    "${headerPrefix} @last-modified ${lastModifiedDate}",
                    "${headerEnd}"
                ]
            }
        },
        {
            "python": {
                "headerBegin": "# -*- coding: utf-8 -*-",
                "headerPrefix": "#",
                "headerEnd": "#",
                "template": [
                    "${headerBegin}",
                    "${headerPrefix} ${fileName}",
                    "${headerPrefix} @author ${author}",
                    "${headerPrefix} @description ${description}",
                    "${headerPrefix} @created ${createdDate}",
                    "${headerPrefix} @last-modified ${lastModifiedDate}",
                    "${headerEnd}"
                ]
            }
        }
    ],
    "editor.fontFamily": "monospace",
    "terminal.integrated.fontFamily": "monospace",
    "editor.fontWeight": "500",
}

配置完畢,可以在vscode中愉快的寫python了。