简体   繁体   中英

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?

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

Ie: 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM