Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lord-haffi opened this issue Oct 1, 2024 · 0 comments
Labels
Import system Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning

Comments

@lord-haffi
Copy link

lord-haffi commented Oct 1, 2024

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

@lord-haffi lord-haffi added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Oct 1, 2024
@Pierre-Sassoulas Pierre-Sassoulas added Import system Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Import system Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning
Projects
None yet
Development

No branches or pull requests

2 participants