Search code examples
pythonvisual-studio-codeflake8

Flake8 Complaining "Undefined Variable" About Built-in Functions


I selected flake8 for my project on VSCode, which installed it into my virtualenv. It complains about many built-in functions, like open:

complains about open

I assume it is because VSCode runs flake8 in virtualenv and it does not see built-in functions?

How can I use flake8 in my virtualenv and remove these annoying warnings? I would like to keep flake8 in my environment because I also share it via requirements, with a strict version definition.

Thanks in advance.


Environment

  • VSCode 1.38.1
  • Manjaro 18.1.0
  • Python 3.7.4
  • flake8: 3.7.8 (mccabe: 0.6.1, pycodestyle: 2.5.0, pyflakes: 2.1.1) CPython 3.7.4 on Linux

Solution

  • I do not know what's causing this error. I assumed that VSCode selects flake8 on virtualenv, but it seems not. Let me give the solution first, then elaborate.

    You need to open your user settings (find it via command pallette, CTRL+P) and search for flake8.

    flake8 settings

    We are interested in "Flake8 Path" settings, which defaults to "flake8" value as seen in the screenshot.

    I thought it would take flake8 from virtualenv but it does not. We need to change its value to ${workspaceFolder}/.venv/bin/flake8 (change .venv to your generic virtualenv folder), which will get rid of the error.

    So the only mystery remaining is that where VSCode finds flake8. I do not have flake8 installed neither globally or as user, but since we solved the problem, it should not be a matter of understanding anyway.