Open8
Python の開発環境を作る
2018年までは研究で Python を書いていたけど、その後の Python のエコシステムを知らない
簡単な API サーバを建てる環境を整えるまでのログ
パッケージマネージャーは poetry がいい、という風のうわさを聞いたのでこれを使う
linter/formatter あたりは何を使えばいいかわからずに困っていたけど↓の記事に従うので良さそうだった
poetry add -D flake8 black isort mypy pytest
reportMissingModuleSource
が発生している
これを出力しているのは https://github.com/microsoft/pylance-release が原因
poetry env info
を使うと、poetry がパッケージをインストールしているパスがわかる
3番目の「プロジェクト直下で仮想環境を作成するようにPoetryの設定を変更する」が良さそう
poetry.toml
を作成して次の内容を書く
poetry.toml
[virtualenvs]
in-project = true
仮想環境を作り直す
poetry env remove ${name}
poetry install
:yoshi:
ファイルの保存時に自動的にフォーマットされてほしいので VSCode の設定をいじる
.vscode/settings.json
{
"python.linting.enabled": true,
"python.linting.flake8Enabled": true,
"python.linting.pylintEnabled": false,
"python.linting.lintOnSave": true,
"python.formatting.provider": "black",
"editor.formatOnSave": true
}
isort と mypy の設定もしておく
.vscode/settings.json
{
"python.linting.enabled": true,
"python.linting.flake8Enabled": true,
"python.linting.pylintEnabled": false,
"python.linting.lintOnSave": true,
"python.formatting.provider": "black",
"editor.formatOnSave": true,
"python.linting.mypyEnabled": true,
"[python]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
}
}
"python.linting.flake8Path": "./.venv/bin/pflake8",
もないと、pyproject.toml の内容を読み込んでくれなかった
mypy のために pandas-stubs を入れようとしたら怒られた
poetry add pandas-stubs
と同じ原因っぽい?
pyproject.toml
[tool.poetry.dependencies]
python = "^3.10"
の "^3.10"
を "3.10"
にするとインストールできた
アプリケーションを作る予定なのでバージョン固定で問題ないはず