Background
I have a Python application dependent upon another package which is provided as a git submodule, yielding a directory structure similar to the following:
foo/
bar/
bar/
__init__.py
eggs.py
test/
setup.py
foo/
__init__.py
ham.py
main.py
Accessing the foo
package is simple enough, as main.py
is executed from the toplevel foo/
directory; but the bar
package is nested within another bar
directory and is not directly importable.
This is solvable readily enough, by modifying sys.path
at the beginning of main.py
:
import sys
# Or sys.path.append()
sys.path.insert(0, './bar')
from bar.eggs import Eggs
from foo.ham import Ham
(Note: this code example assumes that main.py
will always be invoked from foo/
; in cases where this may not be the case, '.bar'
could be replaced with os.path.join(os.path.dirname(__file__), 'bar')
though this is clearly more unwieldy.)
The Problem
Unfortunately, pylint doesn't like this solution. While the code works, the linter considers the sys.path
modifications to be a block of code ending the "top of the module" and gives an undesirable wrong-import-position
warning:
C: 6, 0: Import "from bar.eggs import Eggs" should be placed at the top of the module (wrong-import-position)
C: 7, 0: Import "from foo.ham import Ham" should be placed at the top of the module (wrong-import-position)
Similar questions
Adding a path to sys.path in python and pylint
This questioner has an issue with pylint failing to correctly parse the imports altogether. The lone answer to the this question suggests adding to pylint's internal path; this does nothing to avoid complaints about an interleaved sys.path
modification.