Chris Macaluso
Chris Macaluso

Reputation: 1482

How to use Flake8 in VSCode?

My VSCode is using a locally installed anaconda environment, at the default directory, which places it in Program Files. Because of this I'm unable to install flake8 through VSCode, I get a permission error. If I update my conda environment at the local level to install flake8, does anyone know if VSCode recognize and be able to use the package then?

Upvotes: 38

Views: 78755

Answers (7)

mouwsy
mouwsy

Reputation: 1933

As of the date of this answer, I recommend to use the ruff extension of VSCode (alternatively, installing the ruff package in your environment is also possible). The ruff FAQ state:

Ruff can be used as a drop-in replacement for Flake8 when used (1) without or with a small number of plugins, (2) alongside Black, and (3) on Python 3 code.

Checking with https://www.flake8rules.com, your pyproject.toml file should then contain:

[tool.ruff.lint]
select = ["E", "F", "W", "C90"]

The advantage is that ruff is much faster and more common than flake8.

In the quote, plugins mean flake8 plugins, e.g. flake8-use-pathlib.

Upvotes: 0

mundo03
mundo03

Reputation: 21

To whoever finds this 5 years later looking for using flake8 with pyproject.toml

Install flake8 and flake8-pyproject

That is it. Reference: https://github.com/microsoft/vscode-flake8/issues/135

Upvotes: 2

neves
neves

Reputation: 39443

VSCode changed the linting settings. The recommended way to do it now is using an extension. So install Flake8 extension.

Newer versions of the extension has an Flake8 linter embedded. So you don't need to install it in your machine.

VSCode documentation about linting improved a lot. Take a look, but is a summary:

  1. Install Flake8 extension
  2. It just works.

BTW: you should create a separated virtual environment for each of your projects. Doing this you'll be able to locally install packages.

Upvotes: 0

I just installed Flake8 extension to VSCode as shown below. *I use Anaconda on Windows 11:

enter image description here

Then, it works properly as shown below:

enter image description here

Upvotes: 2

Abimael Domínguez
Abimael Domínguez

Reputation: 507

Have you tried to:

  1. ctrl + shift + p
  2. write "select linter" then click on it
  3. click on flake8

Maybe that helps.

Upvotes: 19

Yedhin
Yedhin

Reputation: 3199

Usually yeah it will. But if it doesn't work for you, then you can try specifying absolute path to flake8 and enable it explicitly like so :

"python.linting.flake8Enabled": true,  
"python.linting.flake8Path": "path/to/flake8",  

you can even specify path to your conda environment :

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

Upvotes: 36

darthbith
darthbith

Reputation: 19665

Yes, it will. You'll have to install flake8 into each environment that you specify as an interpreter for a project in VSCode.

Upvotes: 4

Related Questions