如何用pylint提高代码质量

需求背景:

Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准和有潜在问题的代码。

Pylint 是一个 Python 工具,除了平常代码分析工具的作用之外,它提供了更多的功能:如检查一行代码的长度,变量名是否符合命名标准,一个声明过的接口是否被真正实现等等。

Pylint 的一个很大的好处是它的高可配置性,高可定制性,并且可以很容易写小插件来添加功能。

项目中需要做代码规范检查,所以研究一下pylint的使用。

pylint使用:

  • 安装pylint inMac OS:

>pip install pylint

https://www.pylint.org/ 其他平台安装方法可参考官网

确认pylint安装成功:

➜  ~ pylint --version
pylint 2.1.1
astroid 2.0.4
Python 3.6.5 (default, Apr 25 2018, 14:26:36)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
  • 生成默认配置文件:

>pylint --persistent=n --generate-rcfile > pylint.conf
No config file found, using default configuration

查看当前目录下,已经生成了名为pylint.conf的文件,该文件中的配置项都是pylint的默认配置,比较大400多行。

  • check单个文件:

>pylint --rcfile=pylint.conf manage.py
  • check结果总览:
************* Module manage
C:  1, 0: Missing module docstring (missing-docstring)
W: 14,12: Unused import django (unused-import)

报告中上述格式为检查结果总览:C表示convention,规范;W表示warning,告警;pylint结果总共有四个级别:error,warning,refactor,convention,可以根据首字母确定相应的级别。1, 0表示告警所在文件中的行号和列号。

  • 4种级别告警汇总:
Messages by category
+-----------+-------+---------+-----------+
|type       |number |previous |difference |
+===========+=======+=========+===========+
|convention |1      |NC       |NC         |
+-----------+-------+---------+-----------+
|refactor   |0      |NC       |NC         |
+-----------+-------+---------+-----------+
|warning    |1      |NC       |NC         |
+-----------+-------+---------+-----------+
|error      |0      |NC       |NC         |
+-----------+-------+---------+-----------+
  • 消息类型统计:
Messages
+------------------+------------+
|message id        |occurrences |
+==================+============+
|mixed-indentation |8           |
+------------------+------------+
|unused-import     |2           |
+------------------+------------+
|invalid-name      |2           |
+------------------+------------+
|redefined-builtin |1           |
+------------------+------------+
  • check整个工程:

    目前看只能一个module一个module的pylint,但是如果在工程根目录下添加init.py文件,即把工程当做一个python包的话,可以对整个工程进行pylint。
pylint api(api为包名称)

重命名pylint.conf为.pylintrc,即不需要每次执行都带上--rcfile参数了:

pylintrc in the current working directory
.pylintrc in the current working directory
If the current working directory is in a Python module, Pylint searches up the hierarchy of Python modules until it finds a pylintrc file. This allows you to specify coding standards on a module-by-module basis. Of course, a directory is judged to be a Python module if it contains an __init__.py file.
The file named by environment variable PYLINTRC
if you have a home directory which isn’t /root:
.pylintrc in your home directory
.config/pylintrc in your home directory
/etc/pylintrc

pylint集成到PyCharm:

打开File->设置对话框:

扫描二维码关注公众号,回复: 2909009 查看本文章

settings.png
单击ToolsExternal Tools,添加External Tool:

Create Tool.png

这样修改完代码后,Tools菜单下就会多出pylint工具了:

pylint.png

如果想要pylint当前文件,只要运行上图的pylint工具即可。

在mac上集成到PyCharm情况

pycharm  --> Preferences

怎么查看python的pip install xx安装的软件的安装路径?

pip freeze命令可以查看用pip安装的软件有哪些

要查看安装路径,在执行一次命令pip install xx,就会告诉你已经安装,安装路径在哪

mac 打不开usr/直接在浏览器查看 

猜你喜欢

转载自blog.csdn.net/u014028063/article/details/82109879