pylint
pylint copied to clipboard
E0401 (import-error): False positive with relative import from file in another module
Bug description
Two files required to reproduce:
-
app/main.py
:# pylint: disable=missing-module-docstring from .lib.a import hello hello()
-
app/lib/a.py
:# pylint: disable=missing-module-docstring # pylint: disable=missing-function-docstring def hello(): print("hello")
Configuration
.pylintrc
:
[MAIN]
source-roots=.
Command used
pylint app
Pylint output
************* Module app.main
app/main.py:2:0: E0401: Unable to import 'lib.a' (import-error)
------------------------------------------------------------------
Your code has been rated at 0.00/10 (previous run: 0.00/10, +0.00)
Expected behavior
No errors. Running python3 -m app.main
prints hello
successfully (and thus the import succedeed)
Pylint version
pylint 3.0.2
astroid 3.0.1
Python 3.11.6 (main, Oct 23 2023, 22:48:54) [GCC 11.4.0]
OS / Environment
Ubuntu 22.04, zsh
Additional dependencies
No response
Any updates on this?
No sorry. Our import resolver is quite brittle so people have a hard time contributing to it.
I fixing this types of import with
[tool.pylint]
init-hook="import os; import sys; abs_path = os.path.abspath(os.path.dirname(__name__)); sys.path.append(os.path.join(abs_path, 'src'))"
and inside src
are my personal packages, but this solution looks weird to me