|
| 1 | +=========================================== |
| 2 | + Running Pylint from another python program |
| 3 | +=========================================== |
| 4 | + |
| 5 | +You can call Pylint from another Python program thanks to the ``Run()`` |
| 6 | +function in the ``pylint.lint`` module |
| 7 | +(assuming Pylint options are stored in a list of strings ``pylint_options``) as: |
| 8 | + |
| 9 | +.. sourcecode:: python |
| 10 | + |
| 11 | + import pylint.lint |
| 12 | + pylint_opts = ['--disable=line-too-long', 'myfile.py'] |
| 13 | + pylint.lint.Run(pylint_opts) |
| 14 | + |
| 15 | +Another option would be to use the ``run_pylint`` function, which is the same function |
| 16 | +called by the command line. You can either patch ``sys.argv`` or supply arguments yourself: |
| 17 | + |
| 18 | +.. sourcecode:: python |
| 19 | + |
| 20 | + import pylint |
| 21 | + |
| 22 | + sys.argv = ["pylint", "your_file"] |
| 23 | + pylint.run_pylint() |
| 24 | + # Or: |
| 25 | + pylint.run_pylint(argv=["your_file"]) |
| 26 | + |
| 27 | +To silently run Pylint on a ``module_name.py`` module, |
| 28 | +and get its standard output and error: |
| 29 | + |
| 30 | +.. sourcecode:: python |
| 31 | + |
| 32 | + from pylint import epylint as lint |
| 33 | + |
| 34 | + (pylint_stdout, pylint_stderr) = lint.py_run('module_name.py', return_std=True) |
| 35 | + |
| 36 | +It is also possible to include additional Pylint options in the first argument to ``py_run``: |
| 37 | + |
| 38 | +.. sourcecode:: python |
| 39 | + |
| 40 | + from pylint import epylint as lint |
| 41 | + |
| 42 | + (pylint_stdout, pylint_stderr) = lint.py_run('module_name.py --disable C0114', return_std=True) |
| 43 | + |
| 44 | +The options ``--msg-template="{path}:{line}: {category} ({msg_id}, {symbol}, {obj}) {msg}"`` and |
| 45 | +``--reports=n`` are set implicitly inside the ``epylint`` module. |
| 46 | + |
| 47 | +Finally, it is possible to invoke pylint programmatically with a |
| 48 | +reporter initialized with a custom stream: |
| 49 | + |
| 50 | +.. sourcecode:: python |
| 51 | + |
| 52 | + from io import StringIO |
| 53 | + |
| 54 | + from pylint.lint import Run |
| 55 | + from pylint.reporters.text import TextReporter |
| 56 | + |
| 57 | + pylint_output = StringIO() # Custom open stream |
| 58 | + reporter = TextReporter(pylint_output) |
| 59 | + Run(["test_file.py"], reporter=reporter, do_exit=False) |
| 60 | + print(pylint_output.getvalue()) # Retrieve and print the text report |
| 61 | + |
| 62 | +The reporter can accept any stream object as as parameter. In this example, |
| 63 | +the stream outputs to a file: |
| 64 | + |
| 65 | +.. sourcecode:: python |
| 66 | + |
| 67 | + from pylint.lint import Run |
| 68 | + from pylint.reporters.text import TextReporter |
| 69 | + |
| 70 | + with open("report.out", "w") as f: |
| 71 | + reporter = TextReporter(f) |
| 72 | + Run(["test_file.py"], reporter=reporter, do_exit=False) |
| 73 | + |
| 74 | +This would be useful to capture pylint output in an open stream which |
| 75 | +can be passed onto another program. |
| 76 | + |
| 77 | +If your program expects that the files being linted might be edited |
| 78 | +between runs, you will need to clear pylint's inference cache: |
| 79 | + |
| 80 | +.. sourcecode:: python |
| 81 | + |
| 82 | + from pylint.lint import pylinter |
| 83 | + pylinter.MANAGER.clear_cache() |
0 commit comments