13

Does anyone know how to extract only the pylint score for a repository?

So, assuming pylint produces the following output:

Global evaluation
-----------------
Your code has been rated at 6.67/10 (previous run: 6.67/10, +0.00)

I would like it to return a value of 6.67.

Thanks,

Seán

Seán
  • 523
  • 2
  • 10
  • 17

5 Answers5

27

You can run pylint programmatically and get to the stats dictionary of the underlying "linter":

from pylint.lint import Run

results = Run(['test.py'], do_exit=False)
# `exit` is deprecated, use `do_exit` instead
print(results.linter.stats['global_note'])
Sameer Kumar
  • 93
  • 3
  • 10
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
1

The thing to keep in mind is that Pylint will try to convert the file name to a module name, and only be able to process the file if it succeeds. Like below

pylint mymodule.py

should always work since the current working directory is automatically added on top of the python path like this

pylint directory/mymodule.py

will work if directory is a python package (i.e. has an init.py file or it is an implicit namespace package) or if "directory" is in the python path.

If you keep init.py.py in your module, pylint will treat as python module and run pylint on it. you can use same above commands.

1

If you just want the score, why not grep do the heavy work?

(py3_venv) pylint script.py | grep rated
Your code has been rated at 2.65/10 (previous run: 7.65/10, +0.00)
Ravi Kumar
  • 21
  • 1
1

If you are using a Unix system, you can use this trick to get the pylint score from a command-line:

pylint script.py | sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p'

If you want to get it in a file, you can just add output redirection to the end of the line, like this:

pylint script.py | sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p' > score.txt

Enjoy

Kaz
  • 1,047
  • 8
  • 18
0

Update: pylint>=2.12:

from pylint.lint import Run

results = Run(['test.py'], do_exit=False)
# `exit` is deprecated, use `do_exit` instead
print(results.linter.stats.global_note)
# `linter.stats['global_note']` is deprecated, use `linter.stats.global_note` instead
Shayan
  • 1,931
  • 1
  • 9
  • 14