flake8 not picking up config file
Asked Answered
D

5

26

I have my flake8 config file in ~/.config/flake8

[flake8]
max-line-length = 100

However when I run flake8 the config file is not picked up. I know that because i still get warnings over lines longer than 79 char.

I'm on redhat, but the same happens on mac.

I use pyenv. Global is 2.7.6 (not even sure this is relevant)

Dunite answered 10/2, 2015 at 16:13 Comment(8)
How do you run flake8?Pullman
Do you have a setup.cfg or tox.ini file in the same repository where you run flake8 ?Trug
I run it from a bash shell (although ultimately I want this to work though vim - i get the same error at the moment either way). I have neither of those files. I want just one global file.Dunite
I run it like: flake8 --max-line-length=100 --ignore=E501 project > flake8_output.txt. How does that work for you?Pullman
How do I do that using vim-flake8 plugin i wonder (and yes it would work assuming i adjust the cmd there)Dunite
my mac os x also has this issue, but on debian it's ok.Carney
The location of config file on RHEL is at ~/.flake8Echinus
Managed to get it working using ~/.flake8 on MacOS. Possibly the documentation is incorrect.Importation
S
23

For anyone running into this more recently: I turns out flake8 4.x no longer supports loading .config/flake8, and seems to have no alternative.

From https://flake8.pycqa.org/en/latest/internal/option_handling.html#configuration-file-management :

In 4.0.0 we have once again changed how this works and we removed support for user-level config files.

As a workaround, you could try passing --append-config ~/.config/flake8 (possibly in a bash alias).

Alternatively (up to flake8 5.0), for code that lives in your homedir, you could create a ~/.flake8 config file, that will be picked up for any project inside your homedir that does not have its own flake8 config. This works because flake8 looks in the current directory (or maybe the directory with the source file) and then looks upwards through the filesystem until it finds a config file (setup.cfg, tox.ini, or .flake8). Note that documentation is a bit vague about this (suggesting it would not stop at the first config file it finds, but at least flake8 4.0.1 behaves like that). Also note that this no longer works in flake8 5.0.0, since that explicitly ignores ~/.flake8:

Fix ignoring of configuration files exactly in the home directory (See also #1617, #1618).

Sap answered 22/4, 2022 at 20:26 Comment(2)
The last paragraph is no longer true. Looking in the home directory was apparently a bug, fixed in flake8 5.0.0 (see the bottom of flake8.pycqa.org/en/latest/release-notes/5.0.0.html).Robrobaina
Thanks for pointing that out, I've updated my answer. Seems like a rather arbitrary limitation they introduced, but well (I left a comment on the PR about that), but that's probably not going to change anything :-pSap
L
9

I had a silly mistake, leaving out the [flake8] tag at the beginning of my configuration file I just spent 2 hours debugging this problem.

Here was my original .flake8 file:

ignore=
    # line too long
    E501,
    #line break after binary operator
    W504

This was the fix:

[flake8]
ignore=
    # line too long
    E501,
    #line break after binary operator
    W504

Obviously this wasn't OP's problem: they have the tag in there. But if I can save one person from my stupidity, I will be happy. Frankly I was almost too embarrassed to post this because it is an "Is your computer plugged in?" level error, but oh well.

Lithosphere answered 30/10, 2021 at 17:59 Comment(2)
Fell in the same trap :-) Your answer is useful, thank youMacrocosm
Similartly, I had a silly copy-past issue: the list of ignored errors should be indented, elsewise the config parameters (including lenght of lines) is not picked-up.Steading
C
6

If you want to use Flake8 with VS Code, then do the following:

  • Install the VS Code extension called flake8
  • Read the documentation of the extension! It tells you to use the setting flake8.args
  • Add your settings to settings.json. Example:
"flake8.args": [
    "--max-line-length=100",
    "--ignore=E501,W503,W504,E203",
    "--max-complexity=10",
  ],
Cullum answered 17/11, 2022 at 13:41 Comment(1)
FINALLY! I've been pulling my hair out for days trying to get my config to work thank you so muchMurdock
S
3

Sharing my mistake in case this can help someone:

I had a similar issue which was simply due to a bad file name: .flake8.txt instead of .flake8.

Correcting resolves the issue.

Steading answered 18/12, 2020 at 16:12 Comment(2)
And, don't forget to check if .flake8 file placed on correct directory, e.g. rather subdir than root dir :)Dahlia
.flake8 is not the user-level config file. It's just flake8 (without the preceding period)Ignition
B
2

This was caused by a regression in pep8 1.6.1 and is resolved in the just released 1.6.2 version.

Bromal answered 15/2, 2015 at 22:11 Comment(4)
No it isn't. flake8-python2 --version 2.3.0 (pep8: 1.6.2, pyflakes: 0.8.1, mccabe: 0.3) CPython 2.7.9 on LinuxDiversity
@JohnTyree, can you give some more information about what config files you are using and what you are expecting (and getting)?Bromal
Using a tox.ini file at the root of a project with ignore and some error codes in it. I still see those errors when running flake8 as shown above. With an older flake8 (2.2.5 (pep8: 1.5.7, mccabe: 0.2.1, pyflakes: 0.8.1) CPython 2.7.6 on Linux) it works. The tox file has nothing else in it. Just a [flake8] section with ignore = ...Diversity
Thanks John, this was a regression in pep8. I've pushed a fix to the pep8 repo that hopefully fixes this, but if you'd be able to help test it for me, you can refer to this post I made to the code-quality mailing list: mail.python.org/pipermail/code-quality/2015-March/000515.htmlBromal

© 2022 - 2024 — McMap. All rights reserved.