LoginSignup
27
22

More than 3 years have passed since last update.

VSCode Python用settings.jsonまとめ

Last updated at Posted at 2019-09-30

筆者のSetting.json関係

下記をコピペすれば、右下にインストールされていないよというポップアップがでるので、
クリックすればインストールされます。
★のpythonの実行パスはここで異なるので、自分のPCに合わせてください!

{
    "files.autoSave": "afterDelay",
    "files.autoSaveDelay": 1000,
    // ファイルの終了時に行末のスペース削除
    "files.trimTrailingWhitespace": true,
    "files.insertFinalNewline": true,
    "python.linting.lintOnSave": true,

    //Tabをスペースキーとして変換
    "[python]": {
        "editor.insertSpaces": true,
        "editor.detectIndentation": true,
        "editor.tabSize": 4
    },
    ★"python.pythonPath": "python.exeの実行パス",
    "python.venvPath": ".venv",
    "python.pipenvPath": "/usr/local/bin/pipenv",
    "python.linting.pylintEnabled": true,
    "python.linting.pep8Enabled": false,
    "python.linting.mypyEnabled": true,
    "python.linting.flake8Enabled": true,
    "python.linting.flake8Args": [
        "--max-line-length=120",
        "--max-complexity","20"
    ],
    "python.formatting.provider": "autopep8",
    "python.formatting.autopep8Args": [
        "--aggressive",
        "--aggressive",
    ],
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        ".",
        "-p",
        "*test.py"
    ],
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.unittestEnabled": true,
    // windowsの場合:"terminal.integrated.shell.windows": "C:\\WINDOWS\\system32\\cmd.exe",
}

LintTool関係

LintToolとは?

読みやすくすることでバグの減少や、人が読みやすくするものです。
Pythonではpep8という標準なスタイルガイドが存在します。

flask8

下記2つのチェックを行う。

  • pep8のチェック
    ex. コメントは#の後に半角スペースが入っているか

  • pyflakesのチェック
    ex. importされたものが使われているかなどのチェック

HPはこちら(https://bitbucket.org/tarek/flake8/src/default/)

setting.jsonの書き方

  • flask8では、1行あたりの文字数の上限変更(デフォルト:80)
  • 複雑度の設定※ 複雑すぎると読めない
    "python.linting.pylintEnabled": false,
    "python.linting.pep8Enabled": false,
    "python.linting.flake8Enabled": true,
    "python.linting.flake8Args": [
        "--max-line-length=120",
        "--max-complexity","20"
    ],

検証

import sys
b = 1
print(b + "a")

#エラーメッセージ
#'sys' imported but unused
#block comment should start with '# '
#missing whitespace around operator

mypy

  • Python用のオプションの静的型チェック※

HPはこちら(http://mypy-lang.org/)

setting.jsonの書き方

    "python.linting.mypyEnabled": true,

検証

※ python には型がないので思いがけないところでエラーが発生するので、型によるチェック追加

下記例はintとstrを足してエラーが発生しています。

b = 1
print(b + "a")

#エラーメッセージ
#Unsupported operand types for + ("int" and "str")

Test

コードの品質が悪ければ見た目が良くても意味がありません。
コードの品質を保つためにテストを行う必要があります。

テストに使えるものは下記3種類ありますが、個人的にunittestで十分なので、ここではunittestのみ書きます。
* pytest
* unittest
* nose

unittest

ユニットテストを実行する。

"python.testing.unittestArgs": [
    "-v",
    "-s",
    ".",
    "-p",
    "*test.py"
],
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true,

その他役に立つ設定

1秒毎にコードを保存

Ctrl+Sをコード書いた後常に行っているが、無駄だなぁと

"files.autoSave": "afterDelay",
"files.autoSaveDelay": 1000,

ファイルの終了時に行末のスペース削除

"files.trimTrailingWhitespace": true,

ファイルの最終行に改行追加

pep8規約にもあっている。

files.insertFinalNewline": true,

Tabをスペースキーとして変換

"[python]": {
    "editor.insertSpaces": true,
    "editor.detectIndentation": true,
    "editor.tabSize": 4
}

参考

https://code.visualstudio.com/docs/python/testing
https://qiita.com/firedfly/items/00c34018581c6cec9b84

27
22
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
27
22