什么是flake8?
flake8是python模块的包装程序,它检查以下三个错误。
- pep8:检查代码样式是否符合PEP8
- pyflakes:检查代码中的逻辑错误(不包括样式检查)
- mccabe:检查代码的循环复杂性(似乎用定义公式量化分支和循环)
关键是要配合建议的错误检查,如果您将其放入,可能会认为它是安全的???(汗水
顺便说一句,似乎软件包模块pep8最近已重命名为pycodestyle,因此,在安装flake8时,pycodestyle将作为相关模块而不是pep8进行安装。
如何在VS Code中安装?
总之,为了通过flake8对VSCode上的代码进行错误检查和格式化,请在VSCode设置中设置以下内容,并为python执行环境(或虚拟环境)设置必要的模块,这可以通过安装来实现它。
VS代码设置
<表格>
tr>
header>
<身体>
tr>
tr>
tr>
tr>
tr>
tr>
tbody>
table>
安装模块
使用以下命令安装所需的模块。
1 2 | pip install flake8 pip install autopep8 |
然后,我将具体描述介绍过程。
禁用pylint
VS Code默认情况下启用了pylint,因此请先将其禁用。
首先,从VS Code左下方的齿轮图标中打开菜单,单击"设置"并打开"设置"。 (从VSCode1.28.x看来,它已从规范更改为直接编辑settings.json文件)。
在搜索栏中输入python.linting.pylintEnabled进行过滤,然后将显示Pylint Enabled设置,如图所示,取消选中它。
顺便说一句,通过取消选中它,它与settings.json中的以下内容同义。
1 | "python.linting.pylintEnabled": false |
如果在设置
后关闭显示的吐司通知,它将不再显示。
另外,启用python linting(" python.linting.enabled":true)(如果默认情况下为true,则无需更改它)。
介绍flake8
搜索
python.linting.flake8Enabled并启用(true)它。
1 | "python.linting.flake8Enabled": true |
如果更改它,则消息" the no no flake8"将在烤面包通知中以英文显示,因此请按原样安装它,或在环境下使用以下命令安装pep8。
1 | pip install flake8 |
设置为在保存文件时检查棉绒
通过过滤和激活搜索栏中的python.linting.lintOnSave,在保存文件时可以进行错误检查。
1 | "python.linting.lintOnSave": true |
将格式设置为自动格式化
右键菜单
设置为使用pep8自动格式化(pycode样式)。
将自动格式化的格式指定为pep8
对于兼容PEP8的自动格式化,请在搜索栏中搜索python.formatting.provider并选择autopep8以指定要在pep8中自动格式化的格式。
(由于flake8的代码样式为pep8(严格来说是pycodestyle),因此请指定根据pep8执行格式化的模块autopep8作为格式化程序。)
1 | "python.formatting.provider": autopep8 |
在设置
的同时,在Python环境中安装autopep8。
1 2 3 4 5 6 7 8 9 10 11 | pip install autopep8 # Collecting autopep8 # Collecting pycodestyle>=2.4.0 (from autopep8) # Using cached https://files.pythonhosted.org/packages/e5/c6/ce130213489969aa58610042dff1d908c25c731c9575af6935c2dfad03aa/pycodestyle-2.4.0-py2.py3-none-any.whl # flake8 3.5.0 has requirement pycodestyle<2.4.0,>=2.0.0, but you'll have pycodestyle 2.4.0 which is incompatible. # Installing collected packages: pycodestyle, autopep8 # Found existing installation: pycodestyle 2.3.1 # Uninstalling pycodestyle-2.3.1: # Successfully uninstalled pycodestyle-2.3.1 # Successfully installed autopep8-1.4.2 pycodestyle-2.4.0 |
autopep8将作为相关模块安装的pycodestyle引用为格式参考。
通过从右键单击菜单中选择"文档格式",可以自动使用pep8格式(pycode样式)。
设置为在保存
时自动格式化
从搜索栏中找到editor.formatOnSave并启用(true)将其设置为自动格式化。
1 | "editor.formatOnSave" : true |
使用上述方法,保存文件时将执行自动格式化。
如果自动格式化不起作用,并且每个模块所需的pycodestyle版本(flake8和autopep8)不同,则lint和自动格式化可能不起作用。在这种情况下,还可以通过使用检查依赖项的模块pipdeptree来检查程序包的依赖项来检查版本冲突,因此请更改为同时满足两个条件的pycodestyle版本,或者如果两个条件都不满足,请更改优先级功能。 pycodestyle版本。
1 | pip install pipdeptree |
运行安装的pipdeptre模块并检查软件包依赖项时,autopep8所需的pycodestyle版本为2.4.0或更高版本,而安装的版本为2.3.1,因此是一致的。
1 2 3 4 5 6 7 8 | pipdeptree # autopep8==1.4.2 # - pycodestyle [required: >=2.4.0, installed: 2.3.1] # flake8==3.5.0 # - mccabe [required: >=0.6.0,<0.7.0, installed: 0.6.1] # - pycodestyle [required: >=2.0.0,<2.4.0, installed: 2.3.1] # - pyflakes [required: >=1.5.0,<1.7.0, installed: 1.6.0] |
当我检查时,flake8已更新为3.6.0,因此,我将尝试更新为最新版本。
1 | pip install flake8 -U |
当我再次检查版本和依赖项时,根据将flake8的版本更新为3.6.0,将pycodestyle的版本更新为2.4.0或更高版本,这与autopep8一致。
1 2 3 4 5 6 7 8 9 10 11 12 | pipdeptree # autopep8==1.4.2 # - pycodestyle [required: >=2.4.0, installed: 2.4.0] # flake8==3.6.0 # - mccabe [required: >=0.6.0,<0.7.0, installed: 0.6.1] # - pycodestyle [required: >=2.4.0,<2.5.0, installed: 2.4.0] # - pyflakes [required: >=2.0.0,<2.1.0, installed: 2.0.0] # - setuptools [required: >=30, installed: 40.4.3] # pipdeptree==0.13.1 # - pip [required: >=6.0.0, installed: 18.1] # wheel==0.32.1 |
如果flake8的最新版本与autopep8的必需pycodestyle不一致,则您可能需要等待一段时间,直到更新了最新版本的flake8或autopep8,然后再进行更新。
如果在安装模块时pycodestyle不一致,或者如果您想记住PEP8,请在保存时关闭自动格式化功能,并且在知道PEP8时,再次更改pycodestyle的一致性。整理并启用自动整形以提高效率。
在flake8错误检查
中删除pycodestyle(pep8)的单行字符限制
标准编码标准PEP8的字符限制为每行79个字符,因此该限制有些严格。我想避免出现80个字符或更多的错误(尽管起初可能不太好),所以我将删除此错误检查。
具体来说,在包装器flake8的运行时参数中指定一个忽略错误的值(E501行太长)。
在"设置"的搜索栏中输入flake8args以过滤项目,然后单击项目的"在settings.json中编辑"以将其打开。
在打开的settings.json中将鼠标光标移动到python.linting.flake8Args时,在左端将显示一个类似笔的编辑图标,单击它,然后单击"复制到设置"以在用户设置,然后在复制的python.linting.flake8Args的括号中键入" --ignore = E501"进行保存。
使用
及更高版本,您应该能够删除字符限制的错误检查。
参考站点
-
https://blog-ja.sideci.com/entry/python-lint-pickup-5tools
-
http://www.atmarkit.co.jp/ait/articles/1711/24/news034_3.html