Python cannot import name <class>

I have been dealing with the import problem for most of the night.

This is a common problem, but not a single previous question answers my problem.

I am using PyDev (Eclipse plugin) and Kivy library (Python library)

I have a file structure configured like this:

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

The "code" is stored in the eclipse folder "MyProject", but this is not a package, so I did not include it.

Files are as follows:

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 

I get an error message:

  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 this module structure so badly, but I think I'm close to how it should look ...

+7
source share
3 answers

There seems to be circular imports. from engine.py you import main_menu_widget , and from main_menu_widget you import engine .

This is explicitly circular import, which is not permitted by python.

+8
source

in the same folder, use the relative name of the package (this is still good):

 from .engine import Engine 
+5
source

Your code directory is a package. Make sure the directory located above it, i.e. C:\MyProject , judging by your error messages, is in your PYTHONPATH.

Open the context menu by selecting your project and right-clicking, then select "Properties". Select PyDev - PYTHONPATH and from there the "Source Folders" tab. Make sure the directory above is listed; if he doesn’t click the Add Source Folder button, select it in the dialog box and click OK.

+1
source

All Articles