Open8

Python の開発環境を作る

odanodan

2018年までは研究で Python を書いていたけど、その後の Python のエコシステムを知らない
簡単な API サーバを建てる環境を整えるまでのログ

odanodan

reportMissingModuleSource が発生している

これを出力しているのは https://github.com/microsoft/pylance-release が原因

poetry env info を使うと、poetry がパッケージをインストールしているパスがわかる

https://zenn.dev/pesuchin/articles/4c128aeb60cb42204311

3番目の「プロジェクト直下で仮想環境を作成するようにPoetryの設定を変更する」が良さそう

poetry.toml を作成して次の内容を書く

poetry.toml
[virtualenvs]
in-project = true

仮想環境を作り直す

poetry env remove ${name}
poetry install

:yoshi:

odanodan

ファイルの保存時に自動的にフォーマットされてほしいので VSCode の設定をいじる

https://qiita.com/tsu_0514/items/2d52c7bf79cd62d4af4a

.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
}
odanodan

isort と mypy の設定もしておく

https://kamatimaru.hatenablog.com/entry/2020/05/04/224543

.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
    }
  }
}
odanodan
  "python.linting.flake8Path": "./.venv/bin/pflake8",

もないと、pyproject.toml の内容を読み込んでくれなかった