sudonym
sudonym

Reputation: 4018

Eclipse PyDev ignores __init__, leading to unresolved module import error

It is unclear how to properly structure our PyDev projects/packages within our git repo so that absolute imports work out as they should. This results in Eclipse throwing an "unresolved import" error that I don't understand.

Currently, the repo looks like this:

REPO/ [repo master]  # Set as source folder (PYTHONPATH)
    package_A/       # Set as source folder (PYTHONPATH)
       __init__.py   # from foo import some_func
       __main__.py   # Absolute import required
       foo.py        # some_func()
    package_B        # Set as source folder (PYTHONPATH)
       __init__.py
       __main__.py
       bar.py
__init__.py          # Empty

package_A/__init__.py looks like this:

from foo import some_func

Now I go to package_A/__main__.py and want to import some_func() from foo:

from foo import some_func       # works
from package_A import some_func # unresolved import error in Eclipse
# The latter case should work due to the initial import in __init__

According this source, the second option should work. The fact that it doesn't means that due to some reason, the content of package_A/__init__.py doesn't have any effect. What is my mistake?

What is the root cause of the above-mentioned unresolved import error?

Upvotes: 0

Views: 154

Answers (1)

Fabio Zadrozny
Fabio Zadrozny

Reputation: 25362

If you want to import package_A, it should not be set as a source folder itself.

I.e.: only REPO should be marked as a source folder (that's the only entry that should be in the PYTHONPATH).

Note that the from foo import some_func will not work in that case... So, you can either write an absolute import from package_A.foo import some_func or a relative import: from .foo import some_func.

Upvotes: 1

Related Questions