简体   繁体   中英

How to get report + score from Pylint when run Python code?

I can't seem to get any return value when running pylint from Python code directly. Running it from the command line generates a nice report, with a summarized score at the bottom.

I've tried putting the return value of "Run" into a variable, and getting it's "reports" field - but it looks like some default template.

this is what I have:

from io import StringIO
from pylint.reporters import text
from pylint.lint import Run


def main():
    print("I will verify you build!")
    pylint_opts = ['--rcfile=pylintrc.txt', '--reports=n', 'utilities']
    pylint_output = StringIO()
    reporter = text.TextReporter(pylint_output)
    Run(pylint_opts, reporter=reporter, do_exit=False)
    print(pylint_output.read())


if __name__ == '__main__':
    main()

I'd expect some report here, but all I get is: " I will verify you build!

Process finished with exit code 0 "

代码没问题,但这是 pylint 中的一个错误,将由此问题解决

I investigated the pylint issue. We ended up keeping current behavior because your code works with a slight adjustment:

def main():
    print("I will verify you build!")
    pylint_opts = ['--rcfile=pylintrc.txt', '--reports=n', 'utilities']
    pylint_output = StringIO()
    reporter = text.TextReporter(pylint_output)
    Run(pylint_opts, reporter=reporter, do_exit=False)
    print(pylint_output.getvalue())

StringIO is a stream that doesn't hide the stream position. Pylint writes to pylint_output correctly, but leaves the cursor at the end of the stream. pylint_output.read() only reads starting at the cursor.

You can call pylint_output.seek(0) to reset the stream position before calling pylint_output.read() . Alternatively, as demonstrated in the code block above, StringIO offers a method getvalue which will return the entire contents of the buffer regardless of cursor position.

The Pylint documentation will be updated with this example, so thanks for raising it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM