1
0

VSCodeでpythonのLinter+Formatterを設定する

Last updated at Posted at 2024-04-09

はじめに

今回は以下を設定する手順をご紹介します!

また今回紹介する設定では、ローカルでのpip installは一切不要となっていてそこも気に入っております!

0. pythonの設定

pythonの拡張をインストールしておいてください。

1. Linterの設定

VSCの拡張から「flake8」「Mypy Type Checker」をインストールします。

以上です。
この時点でpythonファイルに警告が出るようになっていると思います。(なってなかったらコマンドパレットからReload Windowとかしてみてください)

スクリーンショット 2024-04-09 18.30.35.png

2. Formatterの設定

VSCの拡張から「autopep8」をインストールします。

VSCのsettings.jsonに以下を追加します。
VSCのユーザ設定を汚したくない方はプロジェクトルートに.vscode/settings.jsonを追加してそちらに記載してください。

{
  "[python]": {
    "editor.defaultFormatter": "ms-python.autopep8",
    "editor.formatOnSave": true
  },
  "autopep8.args": [
    "--ignore=E501"
  ]
}

以上です。
この時点でpythonファイルを上書きすると勝手にautopep8が走るようになっていると思います。(なってなかったらコマンドパレットからReload Windowとかしてみてください)

3. おまけ

今回の設定をチームで共有したい場合は、.vscodeフォルダを以下のようにしてgit管理するのがお勧めです。

  • extensions.json
{
    "recommendations": [
        "ms-python.python",
        "ms-python.flake8",
        "ms-python.mypy-type-checker",
        "ms-python.autopep8"
    ]
}
  • settings.json
{
  "[python]": {
    "editor.defaultFormatter": "ms-python.autopep8",
    "editor.formatOnSave": true
  },
  "autopep8.args": [
    "--ignore=E501"
  ]
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0