如何从 python 代码执行 pylint 命令。另外,pylint 中的哪些参数可以根据我的需要生成日志消息

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

我想使用 Python 文件来 运行 pylint 命令。 使用我正在使用的命令提示符

python3 -m pylint test.py

除此之外,我想格式化我的消息,以便我以后可以使用 split 方法来分隔参数并像 excel 一样输入报告。还要用更有意义的名称替换代码,例如 C0103。 我在命令提示符下尝试了以下命令,但无法得到正确的答案

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

代码

# 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)

输出

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)

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

您是否考虑过在您的消息模板中放置分隔符?

因为在这里你只是把所有的东西混在一起,这看起来不是很有帮助或有用,但也不难补救:

$ 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)

尽管如果您想以编程方式使用输出,-f 参数可能更有用。 pylint -f json 会将所有诊断转储为 json 对象的数组,例如具有很好命名的属性。

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

根据文档,您想要的模板项是symbol,“消息的符号名称”:

$ 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.

使用 subprocess to 运行 pylint 可能是最简单和最受支持的方法。您可以手动配置 & 运行 pylint.lint.PyLinter 但据我所知,它没有记录,不受支持,绝对令人痛苦,并且容易摔倒(如 pylint 中的崩溃 --- 这是可悲的是常见 --- 将删除整个脚本)。我们曾经在 $dayjob 这样做,然后回到 运行 在子进程中使用 CLI,它更可靠。

我使用 pylint 的一种方式,这样无论我有什么输出日志都有一个分隔符,我可以存储在 .txt 文件中。

除此之外,我 运行 通过 Python 脚本。 将您想要 运行 Pylint 的脚本放在 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