Invoking Pylint programmatically Invoking Pylint programmatically python python

Invoking Pylint programmatically


Take a look at the pylint/epylint.py file which contains two different ways to start Pylint programmatically.

You can also simply call

from pylint.lint import RunRun(['--errors-only', 'myfile.py'])

for instance.


I got the same problem recently.syt is right, pylint.epylint got several methods in there. However they all call a subprocess in which python is launched again. In my case, this was getting quite slow.

Building from mcarans answer, and finding that there is a flag exit, I did the following

class WritableObject(object):    "dummy output stream for pylint"    def __init__(self):        self.content = []    def write(self, st):        "dummy write"        self.content.append(st)    def read(self):        "dummy read"        return self.contentdef run_pylint(filename):    "run pylint on the given file"    from pylint import lint    from pylint.reporters.text import TextReporter    ARGS = ["-r","n", "--rcfile=rcpylint"]  # put your own here    pylint_output = WritableObject()    lint.Run([filename]+ARGS, reporter=TextReporter(pylint_output), exit=False)    for l in pylint_output.read():        do what ever you want with l...

which is about 3 times faster in my case.With this I have been going through a whole project, using full output to check each source file, point errors, and rank all files from their note.


Instead of creating a WritableObject class we can use StringIO. StringIO contains write method.

import systry:    from io import StringIOexcept:    from StringIO import StringIOstdout = sys.stdoutsys.stdout = StringIO()ARGS = ["-r","n", "--rcfile=rcpylint"]r = lint.Run(['../test.py']+ARGS, exit=False)test = sys.stdout.getvalue()sys.stdout.close()sys.stdout = stdoutprint (test.split('\n'))

Source: