62

I split my tests across multiple Python files:

tests
├── __init__.py
├── test_apples.py
└── test_bananas.py.py

I import the tests in the ‘__init__.py’ file:

from test_apples import ApplesTest
from test_bananas import BananasTest

However running Pyflakes on the command-line:

pyflakes .

outputs the following errors:

tests/__init__.py:1: [E] PYFLAKES:'ApplesTest' imported but unused
tests/__init__.py:2: [E] PYFLAKES:'BananasTest' imported but unused
Géry Ogam
  • 6,336
  • 4
  • 38
  • 67
Venkatesh Bachu
  • 2,348
  • 1
  • 18
  • 28
  • 4
    It isn't an error, it's just information that you have unused imports. There isn't any problem with it. – DrTyrsa Dec 08 '11 at 07:49

6 Answers6

109

To ignore all errors F401 (‘imported but unused’) in ‘__init__.py’ files, the option ‘per-file-ignores’ which has been available since version 3.7.0 of Flake8 (a better Pyflakes) is very convenient. It can be used on the command-line:

flake8 --per-file-ignores="__init__.py:F401" .

or in a configuration file (‘.flake8’, ‘setup.cfg’ or ‘tox.ini’):

[flake8]
per-file-ignores = __init__.py:F401
Géry Ogam
  • 6,336
  • 4
  • 38
  • 67
  • 11
    This solution deserves some love as it is perfectly scoped: one issue in one file type. Added the following code snippet to `.flake8rc` and it works like a champ. ``` [flake8] per-file-ignores = __init__.py:F401 ``` – Nomen Nescio Dec 10 '19 at 18:04
  • 1
    I had some quotation issues in VS Code settings. Just in case, the quotes need to be escaped, as so: "python.linting.flake8Args": [ "--per-file-ignores=\"__init__.py:F401\"" ], – Rafael Zayas Apr 11 '20 at 16:47
  • 3
    Works beautifully in VSCode's Flake Args, just pasted `--per-file-ignores="__init__.py:F401"` there and voila. – Julio Cezar Silva Aug 27 '20 at 22:44
  • How would one use this with pylama / pyflakes in the `pyalama.ini` config file. All the flavors I tried did not work. I would like to ignore all unused imports in `__init__.py` files. The only thing that worked was adding `# noqa` to each line I would like to ignore. – Zach Apr 09 '21 at 21:07
  • 1
    python > linting > flake8 args > add item `per-file-ignores = __init__.py:F401` without quotes works fine in vscode :) – David Mendes Sep 19 '22 at 09:27
  • 2
    years after first upvoting this reply, I wish i could do it again. – Rafael Zayas Feb 09 '23 at 15:20
  • I'd previously upvoted @David Mendes response, but as of March 2023, i believe the quotes are required in the VS Code settings, as Julio Silva's comment indicates. – Rafael Zayas Mar 07 '23 at 22:28
17

Sometimes you have to skip a line. According to the current versions docs (flake8 2.4.1) The files that contain

# flake8: noqa

are skipped. This works, and # noga, # pyflakes.ignore not.

horbor
  • 609
  • 7
  • 13
12

In my version of PyFlakes (0.7.3), using __all__ works.

Additionally, to skip a line, you should add # noqa.

dustinfarris
  • 1,340
  • 12
  • 15
3

add # noqa: F401 to the end of your line (F401 is a code of this error)

Example: from django.utils import http # noqa: F401

Sinisa Rudan
  • 561
  • 8
  • 10
2

The imports are interpreted as being unused because your linting tool doesn't understand how it's being used.

The most comprehensive fix is to ensure that these names are clearly exported from your module. Either by setting __all__, which is a mechanism for exhaustively enumerating the exported names, or by giving the imports explicit exported names using as.

from test_apples import ApplesTest as ApplesTest

However, I would generally recommend using a test runner that scans for test cases rather than importing them all, as that gives you more confidence that you're definitely running all of your tests.

Andrew Aylett
  • 39,182
  • 5
  • 68
  • 95
  • The redundant `from foo import x as x` import is the recommended solution in PEP 484 and [`pyright`](https://github.com/microsoft/pylance-release/issues/856#issuecomment-763793949). – Janosh Nov 17 '22 at 00:06
-7

Add # pyflakes.ignore comment on every line you want to ignore (in your case import statements).

Neil G
  • 32,138
  • 39
  • 156
  • 257
ILYA Khlopotov
  • 705
  • 3
  • 5
  • @user1004669 And it shouldn't. You have imports in `__init__.py`. You don't use them in `__init__.py`. That's what PYFLAKES is telling you. Now it's up to you to decide what to do with it. There is no problem here. So you should do nothing. If you still have some doubts check any well-known project and I sure you will get the same message. – DrTyrsa Dec 08 '11 at 08:31
  • 12
    @DrTysa - The messages are annoying and distract from writing good code. They also make it more difficult to see actual useful messages from pyflakes. – Juan Enrique Muñoz Zolotoochin Aug 24 '12 at 22:06
  • 14
    Not sure why this is marked as a correct answer, the statement `# pyflakes.ignore` isn't working. – maksimov Mar 12 '13 at 15:12