0
0

More than 1 year has passed since last update.

VSCodeでPythonを書く時、formatOnSaveを使いつつインポート並べ替えのみ無効にする

Last updated at Posted at 2022-01-24

VSCodeでPythonを書く時、formatOnSaveを使いつつインポート並べ替えのみ無効にしたいことがあったので、設定方法をメモ。

これをどういうケースで使うか

import文より前に式があるPythonコードを書いていたら、フォーマッタによりimport文が全部最上部にまとまってしまい動作しなくなったので、無効にしたいことがあった。

# このコードが
sys.path += [script_dirpath]
from mylib.allobj_deselect import allObjDeselect
# このようにされ、動かなくなった。
from mylib.allobj_deselect import allObjDeselect
sys.path += [script_dirpath]

環境

  • MacBook Pro (16-inch 2019)
  • macOS Monterey 12.0.1

解決策

設定のJSONを開き(command + ctrl + p -> 「基本設定: 設定(JSON)を開く」)、Python関係の設定を以下のようにする。

{
  "[python]": {
    "editor.defaultFormatter": "ms-python.python",
    "editor.formatOnSave": true
  },
  "python.formatting.autopep8Args": ["--ignore","E402"],
}

以上

0
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
0
0