pylint作为python代码风格检查工具,接近 PEP8风格,在使用此方法的过程中,发现不仅能让代码更加规范,优雅,更能 发现 隐藏的bug。
pylint在Pycharm中的2种安装方式;
第一种效果展示(优点:通过点击错误点,自动定位到文件位置,省时):
安装步骤在:https://github.com/leinardi/pylint-pycharm 中;
我的设置如下:
使用方法:单文件 检查:先点击 某个文件内容,再点击 绿色开始的箭头即可;
还可以 整个项目检查,只检查改变的文件 等等;
第二种安装使用效果展示(缺点:需要手动的找到错误文件位置,耗时):
安装步骤在:https://stackoverflow.com/questions/38134086/how-to-run-pylint-with-pycharm
注意点:
使用方法:文件 或文件夹 右击 选择 External Tools => Pylint 即可
2种安装方式总结:
a.通过pycharm的pylint检测 对 忽略检测文件的的 功能失效,此功能 必须 通过命令行才可使用
b.建议使用第一种方式 安装及使用,因为 省时省力
pylint 忽略 文件夹的正确配置及使用操作:
- 示范 项目结构如图 (忽略检查app和 venv 文件夹的所有文件):
- 在配置文件中 的 [MASTER]的ignore 的 添加 app,venv 或者 在命令行 中 如下:pylint --ignore app,venv project_name 即可(注意多个忽略之间用 逗号 隔开);
- 通过命令行(不是pycharm的工具) 运行 脚本即可;命令为 pylint --rcfile=D:\python3.6\pylint_conf\pylint.conf project_name
pylint 禁用某些功能的方法:
在配置文件中的 [MESSAGES CONTROL] 的 disable 添加 想禁用的功能;
如:报错如下;
则 在 disable尾部添加 missing-docstring 即可;
pylint在行级别的代码中 禁用某些功能(也就是 不对所有代码禁用某个检测,只对某行代码禁用某个检测):
教程地址:https://pylint.readthedocs.io/en/latest/user_guide/message-control.html
实例:
在一行代码的 后面 添加注释,便只忽略检查某一行;
在一个文件的 单独一行 使用 则 此行以下 被忽略;
pylint的配置:
注意点:pylint默认的配置 无法 满足实际的开发需求,需要自行调整;如下代码 为 自行调整的结果;
[MASTER] # A comma-separated list of package or module names from where C extensions may # be loaded. Extensions are loading into the active Python interpreter and may # run arbitrary code. extension-pkg-whitelist= # Add files or directories to the blacklist. They should be base names, not # paths. 选择需要忽略检查的文件夹(在pycharm中使用pylint工具忽略文件夹无效,只能在命令行中生效:pylint --ignore weixin,lib project_name) ignore=app,venv,install_file,libs # Add files or directories matching the regex patterns to the blacklist. The # regex matches against base names, not paths. ignore-patterns= # Python code to execute, usually for sys.path manipulation such as # pygtk.require(). #init-hook= # Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the # number of processors available to use. jobs=1 # Control the amount of potential inferred values when inferring a single # object. This can help the performance when dealing with large functions or # complex, nested conditions. limit-inference-results=100 # List of plugins (as comma separated values of python modules names) to load, # usually to register additional checkers. load-plugins= # Pickle collected data for later comparisons. persistent=no # Specify a configuration file. #rcfile= # When enabled, pylint would attempt to guess common misconfiguration and emit # user-friendly hints instead of false-positive error messages. suggestion-mode=yes # Allow loading of arbitrary C extensions. Extensions are imported into the # active Python interpreter and may run arbitrary code. unsafe-load-any-extension=no [MESSAGES CONTROL] # Only show warnings with the listed confidence levels. Leave empty to show # all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED. confidence= # Disable the message, report, category or checker with the given id(s). You # can either give multiple identifiers separated by comma (,) or put this # option multiple times (only on the command line, not in the configuration # file where it should appear only once). You can also use "--disable=all" to # disable everything first and then reenable specific checks. For example, if # you want to run only the similarities checker, you can use "--disable=all # --enable=similarities". If you want to run only the classes checker, but have # no Warning level messages displayed, use "--disable=all --enable=classes # --disable=W". 禁用pylint的某些模块 如 显示: Missing class docstring (missing-docstring) 则 添加 括号中的 missing-docstring 到变量中 disable=print-statement, parameter-unpacking, unpacking-in-except, old-raise-syntax, backtick, long-suffix, old-ne-