WSL2 + VSCode + DevContainer環境で Python + FastAPIの勉強を始めました。
Udemyの動画授業を見ながら、講師に言われるがままソースを少し書いては動かす。
少し修正しては動かすの繰り返し。
from fastapi import FastAPI
from typing import Optional
で動くソースの間は問題なかったのに、
from Pydantic import BaseModel
を書いた瞬間から急にエラー。
インストールされているのになぜ?パスが通っていない?
DevContainer(コンテナ)だから? VSCodeだから?
と、あれこれ悩むこと1日。
× from Pydantic import BaseModel
○ from pydantic import BaseModel
解決してみれば原因は超しょうもないことなのですが、
ふだんWindows環境で開発していて大文字小文字に無頓着だったせいではまってしまいました…。
参考:VSCodeのPylanceの設定について。
With the default configurations, you will get support for autocompletion, but Pylance might not check for type errors.
You can enable type error checks from Pylance with these steps:Open the "User Settings"
Search for Type Checking Mode
You will find an option under Python › Analysis: Type Checking Mode
Set it to basic or strict (by default it's off)
デフォルトの設定だと型エラーのチェックはしてくれない。
下記の設定を変更するとチェックしてくれるようになる。
VSCodeの設定 "Checking Mode"で検索。
Python › Analysis: Type Checking Mode
basic か strict に変更する。(デフォルトはoff)