niranjan patidar
niranjan patidar

Reputation: 149

How can I execute pylint command from python code. Also what argument in pylint can make log message as per my need

I have a scenario when I want to run the pylint command using a Python file. Using command prompt i am using

python3 -m pylint test.py

Apart from this i want to format my message such that i can later use split method to separate the arguments and feed in report like excel. Also replace code's like C0103 with more meaningful name. I tried below command from command prompt but could not get proper answer

python3 -m pylint --msg-template="{module}{obj}{line}{column}{msg}" init.py

Code

# all of the following are equivalent
my_string = 'Hello'
print(my_string)

my_string = "Hello"
print(my_string)

my_string = '''Hello'''
print(my_string)

# triple quotes string can extend multiple lines
my_string = """Hello, welcome to
           the world of Python"""
print(my_string)

Output

python3 -m pylint init.py
************* Module init
init.py:14:0: C0304: Final newline missing (missing-final-newline)
init.py:1:0: C0114: Missing module docstring (missing-module-docstring)
init.py:2:0: C0103: Constant name "my_string" doesn't conform to UPPER_CASE naming style (invalid-name)
init.py:5:0: C0103: Constant name "my_string" doesn't conform to UPPER_CASE naming style (invalid-name)
init.py:8:0: C0103: Constant name "my_string" doesn't conform to UPPER_CASE naming style (invalid-name)
init.py:12:0: C0103: Constant name "my_string" doesn't conform to UPPER_CASE naming style (invalid-name)

-------------------------------------------------------------------
Your code has been rated at 2.50/10 (previous run: -1.39/10, +3.89)

Upvotes: 2

Views: 3772

Answers (2)

niranjan patidar
niranjan patidar

Reputation: 149

One of the way i use pylint so that whatever output log i have has a separator and i could store in .txt file.

Apart from this i run this through Python script. Here put your script against which you want to run Pylint in script_name.

import os    
script_name = <source_script>     #example test.py
output_file = <output_pylint_txt> #example test.txt

param1 = 'python -m pylint --max-line-length=400 -d relative-beyond-top-level,wildcard-import --msg-template="{abspath}||{path}||{symbol}||{msg_id}||{line}||{column}||{msg}" --reports=y '+ script_name

 param2 = ' > '+output_file
 param = param1 + param2
 os.system(param)   # this will execute final command on command prompt

Upvotes: 0

Masklinn
Masklinn

Reputation: 42227

Apart from this i want to format my message such that i can later use split method to separate the arguments and feed in report like excel.

python3 -m pylint --msg-template="{module}{obj}{line}{column}{msg}" init.py

Have you considered just putting separators in your message template?

Because here you're just smushing all the items together whic doesn't seem very helpful or useful, but isn't hard to remediate either:

$ python3 -m pylint --msg-template="{module}{obj}{line}{column}{msg}" test.py
************* Module test
test10Missing module docstring
test20Constant name "my_string" doesn't conform to UPPER_CASE naming style

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

$ python3 -m pylint --msg-template="{module}|{obj}|{line}|{column}|{msg}" test.py
************* Module test
test||1|0|Missing module docstring
test||2|0|Constant name "my_string" doesn't conform to UPPER_CASE naming style

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

Although the -f parameter is probably more useful if you want to programmatically consume the output. pylint -f json will dump all diagnostics as an array of json object with nicely named properties for instance.

Also replace code's like C0103 with more meaningful name. I tried below command from command prompt but could not get proper answer

From the documentation, the template item you want is symbol, the "symbolic name of the message":

$ pylint test.py
************* Module test
test.py:1:0: C0114: Missing module docstring (missing-module-docstring)
test.py:2:0: C0103: Constant name "my_string" doesn't conform to UPPER_CASE naming style (invalid-name)

------------------------------------------------------------------
Your code has been rated at 0.00/10 (previous run: 4.29/10, -4.29)

$ pylint --msg-template='{msg_id}' test.py
************* Module test
C0114
C0103

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

$ pylint --msg-template='{symbol}' test.py
************* Module test
missing-module-docstring
invalid-name

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

I have a scenario when i want to run the pylint command using a Python file.

Using subprocess to run pylint is probably the simplest and best-supported way to do so. You can configure & run pylint.lint.PyLinter by hand but as far as I know it's undocumented, unsupported, an absolute pain in the ass, and tends to fall over easily (as crashes in pylint --- which are sadly common --- will take down the entire script). We used to do it that way at $dayjob and went back to running the CLI in a subprocess, it is much more reliable.

Upvotes: 1

Related Questions