使用於vscode

  • install python
  • “Ctrl + p”,
    ext install ms-python.python(安裝完成記得重新開啟)

black

自動縮排格式調整

  1. 於本機安裝"black"
    pip3 install black

  2. 進入vscode開啟設定
    快捷:"Ctrl + ," or 點擊左下角齒輪進入setting

  3. 輸入以下並打勾 Format On Save
    format on save

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  4. 搜尋python formatting provider並選取black

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  5. 完成設定 當專案儲存時會自動執行black功能

isort

按字母表順序對import進行排序,自動分成多個部分。

  1. 於本機安裝"isort"
    pip3 install isort

  2. 進入vscode,快捷:"Ctrl +shift+ p" 輸入Preferences:Open setting (JSON)

  3. 開始設定jason(找有缺的補上)

{
    "editor.formatOnSave": true,
    "python.pythonPath": "./workspace_venv/bin/python3",
    "python.sortImports.args": [
        "-rc",
        "-sp isort.cfg"
    ],
    "python.formatting.provider": "black",
    "[python]": {
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        }
    }
}
  1. 完成設定 當專案儲存時會自動執行isort功能

pylint

代碼風格提醒及評分

  1. 快捷:"Ctrl + shift+p"
    輸入select linter

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  2. 選擇Select Linter
    若跳出錯誤請安裝pylint

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  3. 完成
    pylint出现红色波浪線,代表代碼有錯誤,移動滑鼠可直接看到建議(儲存時觸發"Ctrl + s")
    在儲存檔案時會於vscode下方problems、output輸出結果

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

mypy

mypy是Python的靜態類型檢查器,檢查Python各類變量是否類型正確工具

  1. 使用方式參照上述pylint方法

  2. 選擇mypy後會在專案資料夾中出現.vscode資料夾
    (vscode以資料夾管理專案,可於Explorer設定目錄)

  3. 進入setting .json將"python.linting.pylintEnabled"設定為"true"

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  4. 在儲存檔案時會於vscode下方problems、output同時輸出mypy及pylint結果。

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

poetry

一個Python虛擬環境、相依性的管理工具,包含打包及發布,可同時管理python庫與python程序。環境方面區分為一般環境以及發環境。

  • 先透過poetry addorpoetry install檢查是否已創建虛擬環境,若無則會自動創建虛擬環境。以下分成"已存在專案資料夾"及"新增專案資夾"兩種介紹。
  1. 於本機安裝"poetry"
    pip3 install poetry
    若無法下載或使用,可使用以下方式安裝
    curl -SSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python或是於poetry中更新poetry self:update

  2. 已經存在專案資料夾
    (0)先clone或切換到目標資料夾中
    (1)初始化
    poetry init初始化資料夾,產生toml檔案

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    (2)修改config
    poetry config virtualenvs.in-project true
    可以用poetry config --list檢查
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    (3)建立虛擬環境
    poetry install建立環境會生成一個.venv資料夾
    (4)vscode會自動偵測到你建立的虛擬環境並詢問是否要切換過去,點選yes即可進入虛擬環境
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • 若沒有顯示自動切換功能,可使用快捷鍵"shift+ctrl+p"開啟設定並輸入"Python:Select interpreter",輸入interpreter path/home/c95sbh/<你的專案資料夾名稱>/.venv/bin/<你的python版本>,此時產生一個.vscdoe資料夾,其中包含了一個setting.jason檔案,裡面可以看到你設定的路徑
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    之後開啟專案只要選擇該python interpreter就會自動啟動虛擬環境
  1. 無專案資料夾
    (1)建立資料夾
    poetry new <資料夾名稱>
    新增完後會發現檔案中增加了以下幾個檔案,代表建立成功
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    接下來的步驟就接續"1. 已經存在專案資料夾"的步驟"(2)修改config"繼續操作即可(不需要再重新init一次)
Pycharm 中設定虛擬環境方法

安裝好poetry後,我們執行poetry add 或 poetry install 時poetry會檢查是否已經建立了虛擬環境,如果沒有創建,會自動創建虛擬環境,以下對兩種情況進行展示。
1.已存在專案資料夾
項目已存在,或从GitHub中拷貝的項目,先要确定一下項目中是否有pyproject.toml 文件,如果没有我們先執行poetry init 初始化一下項目生成 pyproject.toml 文件,再執行poetry install創建虛擬環境並安裝全部依賴庫,然後再將這個虛擬環境添加到pycharm中:


選擇創建的虛擬環境,比如我这个是:C:\Users\LubanDev\AppData\Local\pypoetry\Cache\virtualenvs\poetrytest-cSc-mkRW-py3.7\Scripts\python.exe,選中後確定即可。

2.新建專案資料夾
如果項目和虛擬環境都沒創建,首先創建新的Python項目:

先選擇系统的python解釋器,創建項目後,執行 poetry init 創建pyproject.toml 文件,或者透過poetry new創建,然後執行 poetry install 創建虛擬環境,之後按第一點,添加已經存在的虛擬環境到對應項目即可。
如下是已經使用poetry虛擬環境後的狀態

來源:https://my.oschina.net/u/4626630/blog/4526576


  • 測試code
#測試isort(按字母表順序對import進行排序,自動分成多個部分。) #測試pylint(代碼風格提醒及評分) import math, sys; import zz; import s; import z; import a; #測試black(自動縮排格式調整) def example1(): ####This is a long comment. This should be wrapped to fit within 72 characters. some_tuple=( 1,2, 3,'a' ); some_variable={'long':'Long code lines should be wrapped within 79 characters.', 'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'], 'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1, 20,300,40000,500000000,60000000000000000]}} return (some_tuple, some_variable) #測試mypy(靜態類型檢查器,檢查Python各類變量是否類型正確工具) from typing import List class Student(): def __init__(self, id:int, name:str, age:int, major:str)->None: self.id = id self.name = name self.age = age self.major = major def display(persons:List[Student])->None: for person in persons: print(f'{person.id} {person.name}') dark = Student('1', 'Dark', '19', 'computer') sun = Student('2', 'Sun', 21, 'account') lujun= Student(3, 'lujun', 'MBA', 25) display(dark) display([sun, dark, 'lujun'])
  1. Python projects with Poetry and VSCode. Part 1
  2. Python projects with Poetry and VSCode. Part 2
  3. Python projects with Poetry and VSCode. Part 3

Python Journey (2) - VS Code 基本使用技巧
活用 Visual Studio Code
Setup Black and Isort in VSCode


tags: VScode