从上层应用程序导入包
所以我有一个具有以下结构的应用程序:
main.py
core/__init__.py
core/user_interface.py
core/util/__init__.py
core/util/widgets/__init__.py
core/util/widgets/tab.py
main.py 文件进行导入:
from core import user_interface
运行成功,然后 user_interface 进行导入调用:
import core.util.widgets.tab
文件 tab.py 然后有一个导入调用:
from core import user_interface
最后一次导入失败并返回错误:
ImportError: cannot import name user_interface
由于导入链中断,应用程序的执行或尝试从终端导入 user_interface 模块会失败并出现此错误。我知道我在某个地方犯了一个非常基本的错误,但我对这件事已经束手无策了。如果有人可以帮助解决这个问题,我将非常感激。
So I have an application with a structure of:
main.py
core/__init__.py
core/user_interface.py
core/util/__init__.py
core/util/widgets/__init__.py
core/util/widgets/tab.py
The main.py file makes an import of:
from core import user_interface
This runs successfully, then user_interface makes an import call:
import core.util.widgets.tab
The file tab.py then has an import call:
from core import user_interface
This last import fails and returns the error:
ImportError: cannot import name user_interface
Execution of the application or attempts to import the user_interface module from the terminal fail with this error since the chain of imports is breaking. I know I'm making a very basic mistake somewhere, but I am about at my wit's end with this thing. If anyone can help resolve this I'd be very grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚遇到了完全相同的问题 - 但只有当模块被称为
core
时才会出现。 (我通过重命名模块证明了这一点,并且工作正常。重命名回 core 并再次引发 ImportError 异常)。我对 python 相当陌生,但得出的结论是,这是由于我的 Python 路径上已经存在另一个名为 core 的模块(尽管我找不到)。
我通过简单地为我的模块(或者更确切地说 Django 应用程序)选择一个不同的名称来解决这个问题
I've just experienced the exact same problem - but it only appears if the module is called
core
. (I proved this by renaming the module and it worked fine. Renaming back tocore
and theImportError
exception is thrown again).I'm fairly new to python but have concluded it's due to another module called
core
already existing on my Python path (although I can't find one).I solved this by simply choosing a different name for my module (or rather Django app)
这是循环导入。您正在从
user_interface
执行import core.util.widgets.tab
,然后尝试从tab
内导入user_interface
。这是一个永远无法完成的导入,取决于彼此的性质。本文更详细地讨论了它们:http://effbot.org/zone/ import-confusion.htm#circular-imports。This is a circular import. You are doing
import core.util.widgets.tab
fromuser_interface
and then trying to importuser_interface
from withintab
. It's an import which can never be completed do to the nature of each depending on the other. This article talks about them in more detail: http://effbot.org/zone/import-confusion.htm#circular-imports.