Python无法导入名称< class> [英] Python cannot import name <class>

查看:348
本文介绍了Python无法导入名称< class>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力解决导入错误,大部分时间都在摔跤。

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.

我正在使用PyDev(一个Eclipse插件)和库Kivy(一个Python库)

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保存在eclipse文件夹MyProject中,但它不是包,所以我没有包含它。

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

文件看起来像这样:

main.py

# main.py
from code.engine import Engine

class MotionApp(App):
    # Ommited

engine.py

engine.py

# engine.py
from code.main_menu_widget import MainMenuWidget

class Engine():
    # Ommited

main_menu_widget.py

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

推荐答案

似乎有一个循环导入。来自 engine.py
您从 main_menu_widget main_menu_widget c $ c>您正在导入引擎

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

这显然是循环导入,python不允许。

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

这篇关于Python无法导入名称&lt; class&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆