[PYTHON] To maintain code quality in PyCharm

About this article

Target person ☆ People who use Python with PyCharm but do not use automatic formatting ☆ People who use Python but do not use code check tools

By using automatic formatting and code check tools It will improve the quality of the code, so let's set it!

And this time every Python engineer would have used it once I would like to share the recommended settings with PyCharm in the IDE!

Install File Watchers

To format the code when saving the file Install the File Watchers plugin.

Open in the order of Preferences> Plugin. Search for File Watchers from Marketplace and install.

image.png

You need to reboot after installation.

autopep8 It automatically formats your Python code in a PEP8 compliant format.

Installation


pip install autopep8

Select Preferences> Tools> File Watchers.

Press the + button.

image.png

Name: The name you want to set (autopep8) File type: Python Scope: Current File Program: /$PyInterpreterDirectory$/flake8 Arguments: --in-place --aggressive --aggressive $FilePath$

Apply with APPLY from OK!

flake8

Python code check tool

Installation


pip install flake8

Select Preferences> Tools> File Watchers.

Press the + button.

image.png

Name: The name you want to set (flake8) File type: Python Scope: Current File Program: /$PyInterpreterDirectory$/flake8 Arguments: --max-line-length 99 $FilePath$

Apply with APPLY from OK!

Black A more restrictive formatting tool than PEP8

image.png

Name: Name you want to set (Black) File type: Python Scope: Current File Program: /$PyInterpreterDirectory$/black Arguments: $FilePath$

flake8-isort A tool that checks the import order of files

image.png

Name: The name you want to set (isort) File type: Python Scope: Current File Program: /$PyInterpreterDirectory$/isort Arguments: $FilePath$

.editorconfig A tool that keeps your code consistent in any editor.

Create .editorconfig directly under the project.

.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

Recommended Posts

To maintain code quality in PyCharm
Set VS Code to PyCharm.
How to run TensorFlow 1.0 code in 2.0
Migrate from VS Code to PyCharm
How to change editor color in PyCharm
PyCharm code interpolation
Personal notes to doc Python code in Sphinx
How to make Python Interpreter changes in Pycharm
Convert cubic mesh code to WKT in Python
I want to write in Python! (1) Code format check
Add auto-completion to EV3 Micropyhon programming in VS Code
Rewrite Python2 code to Python3 (2to3)
How to use linux commands in Visual Studio Code terminal
Test code to check for broken links in the page
How to use VS Code in venv environment on windows
I tried to summarize the code often used in Pandas
A note on how to load a virtual environment in PyCharm
How to specify command line arguments when debugging in PyCharm
Reference reference for those who want to code in Rhinoceros / Grasshopper
I want to be able to run Python in VS Code
Try HeloWorld in your own language (with How to & code)
Covector to think in function
To flush stdout in Python
Write standard input in code
Part 1 Attempt to code mathematics (∈)
Remote related settings in pycharm
Login to website in Python
Command to generate QR code
PyCharm test code automatic generation
Speech to speech in python [text to speech]
Generate QR code in Python
How to develop in Python
Character code learned in Python
Mastering type hinting in PyCharm
Convert python 3.x code to python 2.x
Post to Slack in Python
[Subprocess] When you want to execute another Python program in Python code
[For beginners] How to implement O'reilly sample code in Google Colab
How to implement Java code in the background of RedHat (LinuxONE)
I wrote the code to write the code of Brainf * ck in python
Articles to read when Blender Python script code doesn't work in 2.80
Get the IPv4 address assigned to the network interface in code (Linux)