23

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)

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
evolution
  • 4,332
  • 5
  • 29
  • 34

5 Answers5

21

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).

Matthijs Kooijman
  • 2,498
  • 23
  • 30
  • 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 https://flake8.pycqa.org/en/latest/release-notes/5.0.0.html). – Kevin Jul 12 '23 at 16:10
  • 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](https://github.com/PyCQA/flake8/pull/1618#issuecomment-1633729129)), but that's probably not going to change anything :-p – Matthijs Kooijman Jul 13 '23 at 07:43
8

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.

eric
  • 7,142
  • 12
  • 72
  • 138
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.

CharlesG
  • 329
  • 3
  • 12
  • And, don't forget to check if .flake8 file placed on correct directory, e.g. rather subdir than root dir :) – DGideas Jun 23 '21 at 09:39
  • 1
    `.flake8` is not the user-level config file. It's just `flake8` (without the preceding period) – Urchin Nov 02 '21 at 15:34
3

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",
  ],
Jabba
  • 19,598
  • 6
  • 52
  • 45
  • 1
    FINALLY! I've been pulling my hair out for days trying to get my config to work thank you so much – Ian Rios Jun 29 '23 at 12:48
2

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

Ian Lee
  • 502
  • 1
  • 5
  • 12
  • 1
    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 Linux` – John Tyree Feb 19 '15 at 23:37
  • @JohnTyree, can you give some more information about what config files you are using and what you are expecting (and getting)? – Ian Lee Feb 23 '15 at 01:14
  • 1
    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 = ...` – John Tyree Feb 23 '15 at 03:53
  • 1
    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: https://mail.python.org/pipermail/code-quality/2015-March/000515.html – Ian Lee Mar 18 '15 at 05:33