37

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
Maggyero
  • 4,569
  • 3
  • 30
  • 49
Venkatesh Bachu
  • 2,030
  • 1
  • 17
  • 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

4 Answers4

71

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
Maggyero
  • 4,569
  • 3
  • 30
  • 49
  • 7
    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
  • 2
    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
10

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

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

dustinfarris
  • 1,290
  • 12
  • 15
9

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
  • 490
  • 6
  • 12
-5

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

Neil G
  • 30,493
  • 34
  • 149
  • 247
ILYA Khlopotov
  • 699
  • 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
  • 13
    Not sure why this is marked as a correct answer, the statement `# pyflakes.ignore` isn't working. – maksimov Mar 12 '13 at 15:12