49

Just like we have "eslint --fix" to automatically fix lint problems in Javascript code, do we have something for pylint too for Python code?

ThinkGeek
  • 4,749
  • 13
  • 44
  • 91
  • 11
    This is an old question, but voting to reopen as I don't really think it is seeking recommendations for books, tools, software libraries, etc; it's simply asking `pylint` has a way to automatically fix issues it finds- I was wondering the same thing and found this question. – Alexander Nied Apr 01 '21 at 19:26
  • Pylint (still) don't have the feature to auto fix, but today there are other alternatives like Ruff (id:charliermarsh.ruff) that can fix things. For an existing codebase with many lint errors, running for example Ruff can give you a running start in fixing all these errors. After that you can continue using Ruff, or switch to Pylint depending on your preferences. – Andreas Lundgren Jul 05 '23 at 06:09

3 Answers3

20

Some of the formatting-errors reported by pylint can be fixed with autopep8, black, or the built-in formatting supported by PyCharm and other IDEs.

erb
  • 14,503
  • 5
  • 30
  • 38
PythonUser
  • 706
  • 4
  • 15
  • 2
    Here is an example command using `autopep8`: `autopep8 --in-place --aggressive --aggressive *.py` – Tyler Chong Jan 03 '22 at 02:17
  • @TylerChong That example does not work in version 1.6.0 as it does not recognize *.py. Could you please provide an updated command that checks all files? – Dan Jul 30 '22 at 02:10
  • 2
    autopep8 fixes only a small number of the errors reported by pylint. My code had a 1.95/10 rating after autopep8, with previous run at 0.39/10. – Lucas Gonze Sep 01 '22 at 00:15
  • AutoPep8 works well enough for me ( I am able to use the custom rules from the internal Google pylintrc file) from this link: https://google.github.io/styleguide/pyguide.html – Anshuman Kumar Feb 11 '23 at 11:57
5

You also have black now. Both black and autopep8 can be integrated into most good editors.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
AntonOfTheWoods
  • 809
  • 13
  • 17
3

There is also autoflake which removes unused variables and imports.

Note that it only removes unused imports from the standard library by default, to ensure that there are no side-effects.

Example usage:

autoflake -r --in-place --remove-unused-variables src/
erb
  • 14,503
  • 5
  • 30
  • 38