How to ignore Pyflakes errors ‘imported but unused’ in ‘__init__.py’ files?
Asked Answered
P

6

78

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
Polyandrist answered 8/12, 2011 at 7:38 Comment(1)
It isn't an error, it's just information that you have unused imports. There isn't any problem with it.Tredecillion
E
-7

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

Equalize answered 8/12, 2011 at 7:47 Comment(3)
@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.Tredecillion
@DrTysa - The messages are annoying and distract from writing good code. They also make it more difficult to see actual useful messages from pyflakes.Marcellusmarcelo
Not sure why this is marked as a correct answer, the statement # pyflakes.ignore isn't working.Ensoul
K
130

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
Kling answered 20/9, 2019 at 13:21 Comment(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 ```Tensiometer
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\"" ],Ananna
Works beautifully in VSCode's Flake Args, just pasted --per-file-ignores="__init__.py:F401" there and voila.Cetology
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.Lamellibranch
python > linting > flake8 args > add item per-file-ignores = __init__.py:F401 without quotes works fine in vscode :)Barden
years after first upvoting this reply, I wish i could do it again.Ananna
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.Ananna
T
20

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.

Tragopan answered 1/7, 2015 at 14:43 Comment(0)
K
15

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

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

Kilter answered 30/9, 2013 at 1:34 Comment(2)
If you are not sure what __all__ means, see this thread: #31079547Interplanetary
Does using __all__ make sense when I do only explicit imports inside __init__.py?Vaccinia
R
8

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

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

Redware answered 20/9, 2022 at 12:53 Comment(0)
S
3

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.

Streetcar answered 16/6, 2022 at 13:28 Comment(1)
The redundant from foo import x as x import is the recommended solution in PEP 484 and pyright.Glyco
E
-7

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

Equalize answered 8/12, 2011 at 7:47 Comment(3)
@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.Tredecillion
@DrTysa - The messages are annoying and distract from writing good code. They also make it more difficult to see actual useful messages from pyflakes.Marcellusmarcelo
Not sure why this is marked as a correct answer, the statement # pyflakes.ignore isn't working.Ensoul

© 2022 - 2024 — McMap. All rights reserved.