Skip to content

Cannot find "dunder module" in package - False-positive No name '__main__' in module 'foo' (no-name-in-module) #9993

Open
@lord-haffi

Description

@lord-haffi

Bug description

Hi everyone,

First, I'm not entirely sure if this is a bug in pylint.
When working with unittests in a separate folder (the sources root is of course appended to pythonpath), it happens that while pylint is able to resolve all imports from my project as intended, it is only unable to find the __main__.py in the foo namespace.

Project structure:

MyProject

├── src
│   └── foo
│       ├── __init__.py
│       ├── __main__.py
│       └── bar.py
└── unittests
    └── test_foo.py

With the files contents:

__main__.py

"""
This module is the main entry point for the package.
"""

def cli():
    """Some entry point function"""

bar.py

"""
Some module-level documentation
"""

def bar_func():
    """Some function-level documentation"""

test_foo.py

"""
Test import behaviour of pylint
"""

# pylint: disable=unused-import
from foo.bar import bar_func
from foo.__main__ import cli

Two things to note here:

  1. The import from foo.bar works without problems. Only the import from foo.__main__ doesn't resolve.
  2. When I add an import into __init__.py like from .__main__ import cli and use from foo import cli in the test, it works. No linter errors in the __init__.py.

So question is: Why is from foo.__main__ import cli bad? Is this a bug in pylint?

Configuration

No response

Command used

pylint .\unittests\test_foo.py

Pylint output

************* Module test_foo
unittests\test_foo.py:6:0: E0401: Unable to import 'foo.bar' (import-error)
unittests\test_foo.py:7:0: E0401: Unable to import 'foo.__main__' (import-error)

Expected behavior

Expect to not throw an error in the test-file. It executes without problems.

Pylint version

Tested on two environments with same result:

pylint 3.2.7
astroid 3.2.4
Python 3.12.4 (tags/v3.12.4:8e8a4ba, Jun  6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)]

and

pylint 3.3.1
astroid 3.3.4
Python 3.12.4 (tags/v3.12.4:8e8a4ba, Jun  6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)]

OS / Environment

Windows 11

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions