ext install ms-python.python
(安裝完成記得重新開啟)自動縮排格式調整
於本機安裝"black"
pip3 install black
進入vscode開啟設定
快捷:"Ctrl + ," or 點擊左下角齒輪進入setting
輸入以下並打勾 Format On Save
format on save
搜尋python formatting provider
並選取black
完成設定 當專案儲存時會自動執行black功能
按字母表順序對import進行排序,自動分成多個部分。
於本機安裝"isort"
pip3 install isort
進入vscode,快捷:"Ctrl +shift+ p" 輸入Preferences:Open setting (JSON)
開始設定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
}
}
}
代碼風格提醒及評分
快捷:"Ctrl + shift+p"
輸入select linter
選擇Select Linter
若跳出錯誤請安裝pylint
完成
pylint出现红色波浪線,代表代碼有錯誤,移動滑鼠可直接看到建議(儲存時觸發"Ctrl + s")
在儲存檔案時會於vscode下方problems、output輸出結果
mypy是Python的靜態類型檢查器,檢查Python各類變量是否類型正確工具
使用方式參照上述pylint方法
選擇mypy後會在專案資料夾中出現.vscode資料夾
(vscode以資料夾管理專案,可於Explorer設定目錄)
進入setting .json將"python.linting.pylintEnabled"設定為"true"
在儲存檔案時會於vscode下方problems、output同時輸出mypy及pylint結果。
一個Python虛擬環境、相依性的管理工具,包含打包及發布,可同時管理python庫與python程序。環境方面區分為一般環境以及發環境。
poetry add
orpoetry install
檢查是否已創建虛擬環境,若無則會自動創建虛擬環境。以下分成"已存在專案資料夾"及"新增專案資夾"兩種介紹。於本機安裝"poetry"
pip3 install poetry
若無法下載或使用,可使用以下方式安裝
curl -SSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python
或是於poetry中更新poetry self:update
已經存在專案資料夾
(0)先clone或切換到目標資料夾中
(1)初始化
poetry init
初始化資料夾,產生toml檔案
poetry config virtualenvs.in-project true
poetry config --list
檢查poetry install
建立環境會生成一個.venv資料夾/home/c95sbh/<你的專案資料夾名稱>/.venv/bin/<你的python版本>
,此時產生一個.vscdoe資料夾,其中包含了一個setting.jason檔案,裡面可以看到你設定的路徑poetry new <資料夾名稱>
安裝好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虛擬環境後的狀態
#測試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'])
pylint
VSCode中使用Pylint检查python代码
GhostofGoes/.pylintrc
Pylint features
Running Pylint
isort
VS Code - sort Python imports automatically
Setup Black and Isort in VSCode
mypy
poetry
Python setup in VS Code with Poetry & Black formatter
對poetry不熟悉的可以先看這三篇
Python Journey (2) - VS Code 基本使用技巧
活用 Visual Studio Code
Setup Black and Isort in VSCode
VScode