10

I've been wrestling most of the night trying to solve an import error.

This is a common issue, but no previous question quite answers my issue.

I am using PyDev (an Eclipse plugin), and the library Kivy (a Python library)

I have a file structure set up like this:

<code>
    __init__.py
    main.py
    engine.py
    main_menu_widget.py

"code" is held within the eclipse folder "MyProject" but it's not a package so I didn't include it.

The files look like this:

main.py

# main.py
from code.engine import Engine

class MotionApp(App):
    # Ommited

engine.py

# engine.py
from code.main_menu_widget import MainMenuWidget

class Engine():
    # Ommited

main_menu_widget.py

# main_menu_widget.py
from code.engine import Engine

class MainMenuWidget(Screen):
    pass

The error I recieve, in full detail, is:

 Traceback (most recent call last):
   File "C:\MyProject\code\main.py", line 8, in <module>
     from code.engine import Engine
   File "C:\MyProject\code\engine.py", line 6, in <module>
     from code.main_menu_widget import MainMenuWidget
   File "C:\MyProject\code\main_menu_widget.py", line 3, in <module>
     from code.engine import Engine

Any idea what I did wrong here? I just renamed my entire folder structure because I screwed up this module structure so bad, but I think i'm close to how it should look....

Braiam
  • 1
  • 11
  • 47
  • 78
MintyAnt
  • 2,978
  • 7
  • 25
  • 37
  • 3
    Possible duplicate of [ImportError: Cannot import name X](http://stackoverflow.com/questions/9252543/importerror-cannot-import-name-x) – Baum mit Augen May 18 '16 at 16:32

3 Answers3

12

There seems to be a circular import. from engine.py you are importing main_menu_widget while from main_menu_widgetyou are importing engine.

That is clearly a circular import which is not allowed by python.

Gaurav Kumar
  • 1,091
  • 13
  • 31
5

it's in the same folder, use a relative package name (it's a good practice to do so anyway):

from .engine import Engine
Brian Dilley
  • 3,888
  • 2
  • 24
  • 24
  • not code.engine? Wouldnt that make more sense, since it's all inside a root "code" package? – MintyAnt Mar 04 '13 at 03:21
  • It's generally a good practice to use relative imports when importing things from your own project. – Brian Dilley Mar 04 '13 at 03:22
  • 1
    Fair enough. I just tried it, no luck, it throws the same error, same line. "cannot import name Engine" – MintyAnt Mar 04 '13 at 03:23
  • 9
    oh, duh - you have a circular dependency, main_menu_widget.py depends on engine.py and vica verca – Brian Dilley Mar 04 '13 at 03:59
  • Yep, that's a circular dependency. It's a shitty singleton too. Thanks, I posted a new question for this, but did find a way to bypass. Wish the error message was a tad more descriptive! http://stackoverflow.com/questions/15216424/python-circular-dependencies-and-singletons – MintyAnt Mar 05 '13 at 05:24
  • If you do relative imports in the file where you do ``if __name__ == "__main__":``, you will get this error: ``SystemError: Parent module '' not loaded, cannot perform relative import`` In that file, it seems you can only do absolute imports. – Juha Untinen Aug 30 '17 at 08:17
1

Your code directory is a package. Ensure that the directory above it, i.e C:\MyProject judging from your error messages, is in your PYTHONPATH.

Open the context menu by selecting your project and clicking your mouse's right button, then select Properties. Select PyDev - PYTHONPATH and from there the Source folders tab. Check that the directory mentioned above is present; if it isn't press Add source folder, select it from the dialogue and press OK.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55