简体   繁体   English

Python 3 中的导入顺序

[英]Import order in Python 3

I'm having a few issues with a number of imports in my program,我的程序中的一些导入有一些问题,

In main.py:在 main.py 中:

from world import *
from dialogue import *
from event import *

In dialogue.py:在对话.py中:

from world import *
from event import *

The class Area is defined in world.py, yet when I try to use the Area class from dialogue.py it returns类 Area 是在 world.py 中定义的,但是当我尝试使用 dialog.py 中的 Area 类时,它返回

builtins.NameError: name 'Area' is not defined

If I change the order of the imports in main.py to如果我将 main.py 中的导入顺序更改为

from dialogue import *
from world import *
from event import *

When I try to access the dialogue class from world.py, I get this当我尝试从 world.py 访问对话类时,我得到了这个

builtins.NameError: name 'Dialogue' is not defined

I thought the order of imports shouldn't have made a difference?我认为进口的顺序不应该有什么不同? How can I access all of my classes from all of my files?如何从我的所有文件访问我的所有类?

The class Area is defined in world.py, yet when I try to use the Area class from dialogue.py it returns类 Area 是在 world.py 中定义的,但是当我尝试使用 dialog.py 中的 Area 类时,它返回

The way you are importing your code is wrong.您导入代码的方式是错误的。 From both modules you are importing with * ;从您使用*导入的两个模块; this confuses the Python, because both modules have a class called Area .这让 Python 感到困惑,因为这两个模块都有一个名为Area的类。

Instead of using * (wild import) import them as modules而不是使用* (wild import) 将它们作为模块导入

import dialogue
import world
import event

d1 = world.Dialogue()
d2 = dialogue.Dialogue()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM