Summary of how to set up major Python Lint (pep8, pylint, flake8)

A little note writing.

When you want to make settings such as "Exclude some warnings from the check target" in each of Organize how to write and place the configuration file.

Reference: [Python] Create pep8 and pylint configuration files to hide some warnings --dackdive's blog The information of flake8 is added to the above blog article.

wrap up

In either case, the configuration file should be created under the ~ / .config / directory. (There are other options) The file name is

And pay attention only to pylint. The format of the configuration file is also different only for pylint.

pep8

Location of configuration files

http://pep8.readthedocs.org/en/latest/intro.html#configuration according to,

If on Windows:  ~\.pep8 Otherwise, if the XDG_CONFIG_HOME environment variable is defined:  XDG_CONFIG_HOME/pep8 Else if XDG_CONFIG_HOME is not defined:  ~/.config/pep8

So if you have a Mac that doesn't have XDG_CONFIG_HOME set

~/.config/pep8

Looks good.

How to write a configuration file

Also at http://pep8.readthedocs.org/en/latest/intro.html#configuration,

~/.config/pep8


[pep8]
ignore = E226,E302,E41
max-line-length = 160

After ʻignore =`, write the code of the warning you want to ignore, separated by commas.

Check the code here. http://pep8.readthedocs.org/en/latest/intro.html#error-codes

pylint

Location of configuration files

https://pylint.readthedocs.io/en/latest/user_guide/run.html#command-line-options According to the pylint configuration file, the first one found is read by searching in the following order.

  1. pylintrc in the current working directory
  1. .pylintrc in the current working directory
  2. If the current working directory is in a Python module, Pylint searches up the hierarchy of Python modules until it finds a pylintrc file. This allows you to specify coding standards on a module-by-module basis. Of course, a directory is judged to be a Python module if it contains an __init__.py file.
  3. The file named by environment variable PYLINTRC
  4. if you have a home directory which isn’t /root:
    1. .pylintrc in your home directory
    2. .config/pylintrc in your home directory
  5. /etc/pylintrc

Roughly translated into Japanese

  1. pylintrc in the current working directory
  2. .pylintrc in the current working directory
  3. If the current working directory is a Python module directory (where __init__.py exists), go up the hierarchy until you find pylintrc. This allows you to set pylintrc on a per-module basis (and modules without files can use pylintrc at the root of the project).
  4. The file specified by the environment variable PYLINTRC
  5. If there is a home directory other than / root:
  6. .pylintrc directly under the home directory 2. $HOME/.config/pylintrc
  7. /etc/pylintrc

What a place, such as. The working directory will probably be the location when you run the pylint command.

I like to put it under the ~ / .config directory like pep8

~/.config/pylintrc

It was made.

How to write a configuration file

If you execute pylint with the --generate-rcfile option, the template of the configuration file will be output, so save it in an appropriate file.

$ pylint --generate-rcfile > ~/.config/pylintrc

In the generated pylintrc file, there is a variable calleddisable =, so write the warning code you want to ignore there.

~/.config/pylintrc


# Disable the message, report, category or checker with the given id(s). You
# can either give multiple identifiers separated by comma (,) or put this
# option multiple times (only on the command line, not in the configuration
# file where it should appear only once).You can also use "--disable=all" to
# disable everything first and then reenable specific checks. For example, if
# you want to run only the similarities checker, you can use "--disable=all
# --enable=similarities". If you want to run only the classes checker, but have
# no Warning level messages displayed, use"--disable=all --enable=classes
# --disable=W"
disable=
  oct-method,
  ext-method-called,
  C0111

You can find the code here. http://pylint-messages.wikidot.com/all-codes

Also, instead of the code, it seems that you can specify a name called symbolic name, which can be guessed more than the code. (The above are ʻoct-method and ʻext-method-called) http://docs.pylint.org/faq.html#do-i-have-to-remember-all-these-numbers

The symbolic name can be found from here based on the code. http://docs.pylint.org/features.html#

Also, as far as I've tried it, the code (or symbolic name) seems to be fine to separate with line breaks.

flake8

Location of configuration files

http://flake8.readthedocs.org/en/latest/config.html According to non-Windows

~/.config/flake8

is. (For Windows ~ / .flake8)

Also, if there is a file called tox.ini or setup.cfg for each project It seems that the settings described there are also automatically loaded. (The writing method is the same as ↓)

How to write a configuration file

Also according to http://flake8.readthedocs.org/en/latest/config.html

~/.config/flake8


[flake8]
ignore = E226,E302,E41
max-line-length = 160
exclude = tests/*
max-complexity = 10

And so on, it's OK in the same format as pep8.

Check the code here. F *** series: http://flake8.readthedocs.org/en/latest/warnings.html ʻE *** , W *** `(pep8) series: http://pep8.readthedocs.org/en/latest/intro.html#error-codes

Recommended Posts

Summary of how to set up major Python Lint (pep8, pylint, flake8)
[Python2.7] Summary of how to use unittest
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
Summary of how to import files in Python 3
Summary of how to use MNIST in Python
How to set up a Python environment using pyenv
[Python] Summary of how to use split and join functions
Summary of how to use pyenv-virtualenv
How to speed up Python calculations
Summary of how to use csvkit
How to set up the development environment of ev3dev [Windows version]
How to speed up instantiation of BeautifulSoup
How to set up SVM using Optuna
Summary of procedures up to PyPI registration
Summary of how to write AWS Lambda
[Question] How to use plot_surface of python
[Python] How to use two types of type ()
Summary of how to read numerical data with python [CSV, NetCDF, Fortran binary]
How to set up Random forest using Optuna
How to specify attributes with Mock of python
How to set up Random forest using Optuna
How to get dictionary type elements of Python 2.7
How to set up a local development server
[Python] Summary of eval / exec functions + How to write character strings with line breaks
How to set up public key authentication in ssh
Summary of tools needed to analyze data in Python
How to get the number of digits in Python
I tried to summarize how to use matplotlib of python
[Blender] Summary of how to install / update / uninstall add-ons
How to use Python Kivy ① ~ Basics of Kv Language ~
[Python] How to write a docstring that conforms to PEP8
[Blender] How to dynamically set the selection of EnumProperty
Review of atcoder ABC158, up to question E (Python)
How to set up and compile your Cython environment
Summary of how to share state with multiple functions
How to install Python
How to install python
Summary of Python arguments
How to set up a simple SMTP server that can be tested locally in Python
How to set up Ubuntu for Windows Subsystem for Linux 2 (WSL2)
[Python] How to make a list of character strings character by character
Offline real-time how to write Python implementation example of E14
Review of AtCoder Beginner Contest 159, up to question E (Python)
Review of AtCoder Beginner Contest 163, up to question E (Python)
How to shuffle a part of a Python list (at random.shuffle)
Review of AtCoder Beginner Contest 164, up to question E (Python)
How to connect to various DBs from Python (PEP 249) and SQLAlchemy
Review of AtCoder Beginner Contest 162, up to question E (Python)
[Linux] [C / C ++] Summary of how to get pid, ppid, tid
[Python Kivy] How to create a simple pop up window
Review of AtCoder Beginner Contest 154, up to question E (Python)
How to develop in a virtual environment of Python [Memo]
Comparison of how to use higher-order functions in Python 2 and 3
How to get a list of built-in exceptions in python
Summary of how to write .proto files used in gRPC
Review of AtCoder Beginner Contest 160, up to question E (Python)
Review of AtCoder Beginner Contest 157, up to question E (Python)
Review of AtCoder Beginner Contest 161, up to question E (Python)
Review of AtCoder Beginner Contest 155, up to question E (Python)
Review of AtCoder Beginner Contest 156, up to question E (Python)