LoginSignup
5
3

More than 3 years have passed since last update.

PyCharmでコードの品質を保つために

Last updated at Posted at 2020-12-31

この記事について

対象の人
☆PyCharmでPythonを使っているけど、自動フォーマットを使っていない
☆Pythonを使っているけど、コードチェックツールを使っていない

自動フォーマットや、コードチェックツールを使うことで
コードの品質が向上するのでぜひ設定しましょう!

そして今回はPythonエンジニアなら誰もが一度は使ったことがあるであろう
IDEのPyCharmでおすすめ設定を共有したいと思います!

File Watchersをインストールする

ファイル保存時にコードのフォーマットなどを行うために
File Watchersプラグインをインストールする。

Preferences > Pluginの順番で開く。
MarketplaceからFile Watchersで検索してインストール。

image.png

インストール後は再起動する必要あり。

autopep8

Pythonのコードを自動的にPEP8に準拠したフォーマットにしてくれる。

インストール
pip install autopep8

Preferences > Tools > File Watchersを選択。

+ボタンを押す。

image.png

Name: 設定したい名前(autopep8)
File type: Python
Scope: Current File
Program: /$PyInterpreterDirectory$/flake8
Arguments: --in-place --aggressive --aggressive $FilePath$

OKからのAPPLYで適用完了!

flake8

Pythonのコードチェックツール

インストール
pip install flake8

Preferences > Tools > File Watchersを選択。

+ボタンを押す。

image.png

Name: 設定したい名前(flake8)
File type: Python
Scope: Current File
Program: /$PyInterpreterDirectory$/flake8
Arguments: --max-line-length 99 $FilePath$

OKからのAPPLYで適用完了!

Black

PEP8より制限のきつめなフォーマットツール

image.png

Name: 設定したい名前(Black)
File type: Python
Scope: Current File
Program: /$PyInterpreterDirectory$/black
Arguments: $FilePath$

flake8-isort

ファイルのインポート順をチェックしてくれるツール

image.png

Name: 設定したい名前(isort)
File type: Python
Scope: Current File
Program: /$PyInterpreterDirectory$/isort
Arguments: $FilePath$

.editorconfig

どんなエディターでも一貫したコードを保つことができるツール。

プロジェクト直下に.editorconfigを作成。

.editorconfig
# http://editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.{py,rst,ini}]
indent_style = space
indent_size = 4

[*.py]
line_length = 88
known_first_party = django,config
multi_line_output = 3
default_section = THIRDPARTY
recursive = true
skip = venv/
skip_glob = **/migrations/*.py
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true

[*.{html,css,scss,json,yml}]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab

[nginx.conf]
indent_style = space
indent_size = 2
5
3
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
5
3