开发者

Import package from upper level of application

开发者 https://www.devze.com 2023-03-05 02:50 出处:网络
So I have an application with a structure of: main.py core/__init__.py core/user_interface.py core/util/__init__.py

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开发者_如何学JAVA

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.


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 to core and the ImportError 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)


This is a circular import. You are doing import core.util.widgets.tab from user_interface and then trying to import user_interface from within tab. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消