Search code examples
pythonpython-3.xlintpylint

Difference between `Run()` and `py_run` and the use of `try except` block in `b.py` in Pylint


This is my code:

# uses Python3.7
# a.py

from pylint import lint as pl

pathvar = 'test.py'
pylint_opts = [pathvar]

pl.Run(pylint_opts)

print('New Text File Here')

This code gives me the correct output but doesn't execute anything after Run statement and hence doesn't execute the print statement. However, If i add a try except block in there it runs fine.

# uses Python3.7
# b.py

from pylint import lint as pl

try:
    pathvar = 'test.py'
    pylint_opts = [pathvar]
    pl.Run(pylint_opts)
except:
    pass

print('New Text File Here')

There is also another method to run pylint on a file from python program:

# uses Python3.7
# c.py

from pylint import epylint as lint

pathvar = 'test.py'
lint.py_run(pathvar)

print('New Text File Here')

This one executes the py_run and then prints the correct output. I know you might suggest that I should use c.py as it already solves my problem of running pylint. But a.py is more general and various arguments can also be passed apart from running the pylint file. Why b.py needs a try except block and c.py doesnt for the print command to execute ?


Solution

  • This is because Run class uses sys.exit in its __init__ method. You can pass do_exit=False argument like pl.Run(pylint_opts, do_exit=False) to make a.py working as you wish: printing after running pylint.