目录结构是:
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.py
,that.py
和PP.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笔记本。
答案 0 :(得分:0)
使用global关键字是不够的。您必须在模块级别声明变量:
#variable.py
globe1 = 8
globe2 = [1,2,3,4,5]
globe3 = "bakar"
def initialise():
pass # nothing to do there anymore.