Python 无法导入名称 <class>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15194010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:29:11  来源:igfitidea点击:

Python cannot import name <class>

pythonimportmodulekivy

提问by MintyAnt

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)

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

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.

“代码”保存在 eclipse 文件夹“MyProject”中,但它不是一个包,所以我没有包含它。

The files look like this:

文件如下所示:

main.py

主文件

# main.py
from code.engine import Engine

class MotionApp(App):
    # Ommited

engine.py

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

知道我在这里做错了什么吗?我刚刚重命名了我的整个文件夹结构,因为我把这个模块结构搞砸了,但我想我已经接近它应该是什么样子了......

回答by Brian Dilley

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

它在同一个文件夹中,使用相对包名称(无论如何这样做是一个好习惯):

from .engine import Engine

回答by Nicola Musatti

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

您的代码目录一个包。确保它上面的目录,即C:\MyProject根据您的错误消息判断,在您的 PYTHONPATH 中。

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

通过选择您的项目并单击鼠标右键打开上下文菜单,然后选择Properties。选择PyDev - PYTHONPATH,然后选择Source folders选项卡。检查上面提到的目录是否存在;如果不是按Add source folder,从对话框中选择它并按OK

回答by Gaurav Kumar

There seems to be a circular import. from engine.pyyou are importing main_menu_widgetwhile from main_menu_widgetyou are importing engine.

似乎有一个循环导入。from engine.pyyou are importing main_menu_widgetwhile from main_menu_widgetyou are importing engine.

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

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