Unresolved import errors yet successful import sti

2019-06-04 18:51发布

问题:

I am working on a project in Eclipse Juno. I wrote a class called Character in a package named chargen.py. There's a red X next to from chargen import Character:

Unresolved import: Character
Character Found at: Avarice_v0.PlayAvarice_v0

from chargen import Character

Yet the import works. The entire code at the moment is simply this:

from chargen import Character

def main():
    PLAYER = Character("")
    print(PLAYER)

if __name__ == '__main__':
    main()

This code results in the printing of the __str__ proving the Character("") ran. Also, it generates no errors when running. Why does Eclipse label this Unresolved import: Character?

回答1:

I figured out how to get rid of the error. I moved everything one directory up. Settings for the project showed the PYTHONPATH included the main directory, but not the nested one. By moving it all up and deleting the now empty original folder, I have no unresolved import errors. This helped me to understand more about the PYTHONPATH choices offered at the initial setup of the project in PyDev.



回答2:

In the properties for your project, there's a pane called "PyDev - PYTHONPATH", with a sub-pane called "External Libraries". You can add source folders (any folder that has an init.py) to the path using that pane. Your project code will then be able to import modules from those source folders.