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"],
}
以上