AttributeError:'模块'对象没有属性' globe1'

时间:2015-06-12 10:00:25

标签: python python-import

目录结构是:

ga/ :

    PP.py

    this.py

    that.py

tripathi/:

    nn.py # contains function net

    neuron.py #contains function node

    aicc.py # contains function AICC

    blah.py

    variable.py

    __init__.py

我正在尝试使用' tripathi'目录作为模块。因此,__init__.py

#__init__.py 

from nn import net

from neuron import node

from aicc import AICC
#variable.py

def initialise (): #to initialise global variables


    global globe1, globe2, globe3

    globe1 = 8

    globe2 = [1,2,3,4,5]

    globe3 = "bakar"

文件variable.py包含一些要在this.pythat.pyPP.py

中使用的全局变量
#this.py

from tripathi import node 

from tripathi import AICC #function AICC in aicc.py

from tripathi import variables #file variables.py

variables.initialise() #all global variables initialised

this_one = variables.globe1

现在,that.py有

#that.py

import this

AttributeError : 'module' object has no attribute 'globe1'

为什么会出错? import this语句(在that.py中)是否导入this.py中导入的所有模块?

编辑: 我正在使用IPython笔记本。

1 个答案:

答案 0 :(得分:0)

使用global关键字是不够的。您必须在模块级别声明变量:

#variable.py

globe1 = 8
globe2 = [1,2,3,4,5]
globe3 = "bakar"

def initialise():
    pass # nothing to do there anymore.