使用其他中间模块无法导入

时间:2020-01-16 18:30:33

标签: python

我正在尝试通过另一个模块导入一个模块。我的代码如下:
main.py

import init
myInsert = Insert()

init.py

from insert import Insert

insert.py

class Insert:
  def __init__(self):
    print('insert is innitiated')<br>

这意味着我正在尝试拥有一个初始化文件来加载以后需要的所有模块。 例如,我尝试通过insert模块加载init模块,然后在main.py中使用它。 不幸的是,当我运行main.py时出现错误,如下所示:

NameError: name 'Insert' is not defined

能否请您告诉我我在做什么错以及如何使其正常工作?

1 个答案:

答案 0 :(得分:2)

这里:

import init
myInsert = Insert()

确实没有定义名称Insert。它从何而来?不是来自init,因为来自该模块的名称将被引用为init.name,因此它必须是全局的。但是它没有在其他任何地方定义(没有像Init = <thing>这样的赋值,没有像from init import *这样的恒星输入),所以这里有一个错误。

您正在寻找:

myInsert = init.Insert()