pylint icon indicating copy to clipboard operation
pylint copied to clipboard

E0401 (import-error): False positive with relative import from file in another module

Open fdelu opened this issue 1 year ago • 3 comments

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

fdelu avatar Nov 25 '23 02:11 fdelu

Any updates on this?

mdragoss avatar Jan 26 '24 16:01 mdragoss

No sorry. Our import resolver is quite brittle so people have a hard time contributing to it.

DanielNoord avatar Jan 30 '24 12:01 DanielNoord

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

mdragoss avatar Jan 30 '24 17:01 mdragoss