flake8在VSCode中无法显示致命错误

8

VSCode无法显示致命错误,它只会在代码中突出显示警告。例如:

我正在使用Python 2.7的虚拟环境运行flake8,但VSCode没有显示致命错误。下面是设置:

"python.linting.flake8Enabled": true,

我正在将vscode“问题”窗口的结果与直接从命令行运行flake8的结果进行比较。
def foo(bar):
    o += 1

    print(bar)

当我在终端上运行flake8对上面的代码进行检查时,会得到所有的linting错误和警告。
> flake8 python/mock.py 
python/mock.py:4:5: F821 undefined name 'o'
python/mock.py:4:5: F841 local variable 'o' is assigned to but never used
python/mock.py:5:1: W293 blank line contains whitespace

当我在VSCode中执行代码检查时,只收到了警告信息。

blank line contains whitespace flake8(W293) [5,1]

我在配置中漏掉了什么吗?有没有办法检查vscode是如何调用flake8的?


1
事实证明问题出在flake8的版本上。使用最新版本无法正常工作,因此我明确设置为使用flake8==3.5,然后它开始按预期工作了。 - Raphael Philipe
2个回答

3

默认配置对我来说很有效(在virtualenv上也适用于Python2.7)。

flake8

请确认:

  • The path to the flake8 executable is explicitly specified in settings.json

    # From terminal/console, install flake8 into your virtual environment
    $ pipenv install --dev flake8
    $ which flake8
    /absolute/path/to/virtualenvs/test-v9MbxBL-/bin/flake8
    
    # Set in settings.json
    "python.linting.flake8Path": "/absolute/path/to/virtualenvs/test-v9MbxBL-/bin/flake8",
    
  • The severity for Fatal and Error categories are set to "Error":

    "python.linting.flake8CategorySeverity.F": "Error",
    "python.linting.flake8CategorySeverity.E": "Error",
    
  • There are no ignored errors:

    "python.linting.flake8Args": [
        "--ignore=F821"
    ]
    
  • There are no overriding flake8 settings from external sources

    Flake8 user options are read from the C:\Users\<username>\.flake8 (Windows) or ~/.config/flake8 (macOS/Linux) file.

    At the project level, options are read from the [flake8] section of a tox.ini, setup.cfg, or .flake8 file.


在 settings.json 中手动指定 flake8 的路径对我很有帮助。 - Luka Sh

0
通常情况下,是的,它会起作用。但如果对您不起作用,那么您可以尝试指定 flake8 的绝对路径并显式启用它,如下所示:
"python.linting.flake8Enabled": true,  
"python.linting.flake8Path": "path/to/flake8",

你甚至可以指定你的conda环境路径:

"python.condaPath": "path/to/condaenv/",

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接