# 使用於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``` ![](https://i.imgur.com/AxceZB4.png) 4. 搜尋```python formatting provider```並選取black ![](https://i.imgur.com/PoZk09x.png) 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 } } } ``` 4. 完成設定 當專案儲存時會自動執行isort功能 ## **pylint** **代碼風格提醒及評分** 1. 快捷:"Ctrl + shift+p" 輸入```select linter``` ![](https://i.imgur.com/GZRNgez.png) 2. 選擇Select Linter 若跳出錯誤請安裝pylint ![](https://i.imgur.com/m9vuZEo.png) 3. 完成 pylint出现红色波浪線,代表代碼有錯誤,移動滑鼠可直接看到建議(儲存時觸發"Ctrl + s") 在儲存檔案時會於vscode下方problems、output輸出結果 ![](https://i.imgur.com/gjrZTzd.png) ## **mypy** **mypy是Python的靜態類型檢查器,檢查Python各類變量是否類型正確工具** 1. 使用方式參照上述**pylint**方法 3. 選擇mypy後會在專案資料夾中出現.vscode資料夾 (vscode以資料夾管理專案,可於Explorer設定目錄) 3. 進入setting .json將"python.linting.pylintEnabled"設定為"true" ![](https://i.imgur.com/yqNFCWg.png) 4. 在儲存檔案時會於vscode下方problems、output同時輸出mypy及pylint結果。 ![](https://i.imgur.com/VsB9csC.png) ## **poetry** **一個Python虛擬環境、相依性的管理工具,包含打包及發布,可同時管理python庫與python程序。環境方面區分為一般環境以及發環境。** * 先透過```poetry add```or```poetry install```檢查是否已創建虛擬環境,若無則會自動創建虛擬環境。以下分成"已存在專案資料夾"及"新增專案資夾"兩種介紹。 0. 於本機安裝"poetry" ```pip3 install poetry``` 若無法下載或使用,可使用以下方式安裝 ```curl -SSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python```或是於poetry中更新```poetry self:update``` 1. **已經存在專案資料夾** (0)先clone或切換到目標資料夾中 (1)初始化 ```poetry init```初始化資料夾,產生toml檔案 ![](https://i.imgur.com/0P42mxx.png) (2)修改config ```poetry config virtualenvs.in-project true``` 可以用```poetry config --list```檢查 ![](https://i.imgur.com/ChKqydI.png) (3)建立虛擬環境 ```poetry install```建立環境會生成一個.venv資料夾 (4)vscode會自動偵測到你建立的虛擬環境並詢問是否要切換過去,點選yes即可進入虛擬環境 ![](https://i.imgur.com/n0P22KV.png) * 若沒有顯示自動切換功能,可使用快捷鍵"shift+ctrl+p"開啟設定並輸入"Python:Select interpreter",輸入interpreter path```/home/c95sbh/<你的專案資料夾名稱>/.venv/bin/<你的python版本>```,此時產生一個.vscdoe資料夾,其中包含了一個setting.jason檔案,裡面可以看到你設定的路徑 ![](https://i.imgur.com/i8NrVlJ.png) 之後開啟專案只要選擇該python interpreter就會自動啟動虛擬環境 2. **無專案資料夾** (1)建立資料夾 ```poetry new <資料夾名稱>``` 新增完後會發現檔案中增加了以下幾個檔案,代表建立成功 ![](https://i.imgur.com/gydOwyh.png) 接下來的步驟就接續"1. **已經存在專案資料夾**"的步驟"(2)修改config"繼續操作即可(不需要再重新init一次) :::spoiler Pycharm 中設定虛擬環境方法 安裝好poetry後,我們執行poetry add 或 poetry install 時poetry會檢查是否已經建立了虛擬環境,如果沒有創建,會自動創建虛擬環境,以下對兩種情況進行展示。 1.已存在專案資料夾 項目已存在,或从GitHub中拷貝的項目,先要确定一下項目中是否有pyproject.toml 文件,如果没有我們先執行poetry init 初始化一下項目生成 pyproject.toml 文件,再執行poetry install創建虛擬環境並安裝全部依賴庫,然後再將這個虛擬環境添加到pycharm中: ![](https://i.imgur.com/Y2T49fL.png) ![](https://i.imgur.com/3diNoWz.png) ![](https://i.imgur.com/saYsYEf.png) 選擇創建的虛擬環境,比如我这个是:C:\Users\LubanDev\AppData\Local\pypoetry\Cache\virtualenvs\poetrytest-cSc-mkRW-py3.7\Scripts\python.exe,選中後確定即可。 2.新建專案資料夾 如果項目和虛擬環境都沒創建,首先創建新的Python項目: ![](https://i.imgur.com/48ggV7N.png) 先選擇系统的python解釋器,創建項目後,執行 poetry init 創建pyproject.toml 文件,或者透過poetry new創建,然後執行 poetry install 創建虛擬環境,之後按第一點,添加已經存在的虛擬環境到對應項目即可。 如下是已經使用poetry虛擬環境後的狀態 ![](https://i.imgur.com/bOsuKoS.png) 來源:https://my.oschina.net/u/4626630/blog/4526576 ::: *** * 測試code ```gherkin= #測試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']) ``` * balck [Setting up python Black on Visual Studio Code]( https://medium.com/@marcobelo/setting-up-python-black-on-visual-studio-code-5318eba4cd00) * pylint [VSCode中使用Pylint检查python代码](https://zhuanlan.zhihu.com/p/262590195) [GhostofGoes/.pylintrc]( https://gist.github.com/GhostofGoes/da3dfda4e6b3882b0fd2cdac676a478c) [Pylint features]( http://pylint.pycqa.org/en/latest/technical_reference/features.html#format-checker) [Running Pylint]( http://pylint.pycqa.org/en/latest/user_guide/run.html) * isort [VS Code - sort Python imports automatically]( https://eshlox.net/2019/12/02/vscode-sort-python-imports-automatically) [Setup Black and Isort in VSCode]( https://medium.com/@cereblanco/setup-black-and-isort-in-vscode-514804590bf9) * mypy * poetry [Python setup in VS Code with Poetry & Black formatter]( https://leoncvlt.com/python-setup-in-vs-code-with-poetry-black-formatter.html) * 對poetry不熟悉的可以先看這三篇 1. [Python projects with Poetry and VSCode. Part 1](https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-1/) 2. [Python projects with Poetry and VSCode. Part 2](https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-2/) 3. [Python projects with Poetry and VSCode. Part 3](https://www.pythoncheatsheet.org/blog/python-projects-with-poetry-and-vscode-part-3/) *** [Python Journey (2) - VS Code 基本使用技巧]( https://www.weithenn.org/2018/05/python-journey-part02-how-to-use-vscode.html) [活用 Visual Studio Code]( https://channel9.msdn.com/Series/Mastering-Visual-Studio-Code) [Setup Black and Isort in VSCode]( https://medium.com/@cereblanco/setup-black-and-isort-in-vscode-514804590bf9) *** ###### tags: `VScode`