在包含多个.py文件的PySpark项目中,有一个名为settings.py
的文件来声明所有全局变量。
# settings.py
def prepareMyList():
return [2,3,4,5]
def setGlobal():
myList = prepareMyList()
global firstEle,secondEle,thirdEle
firstEle = myList[0]
secondEle = myList[1]
thirdEle = myList[2]
现在,另一个文件doStuff.py
导入全局变量。
#doStuff.py
import settings
def incByOne():
settings.firstEle += 1
settings.secondEle += 1
settings.thirdEle += 1
main.py
如下。
#main.py
import settings
import doStuff
settings.setGlobal()
doStuff.incByOne()
print settings.firstEle
项目运行时,它会抛出错误AttributeError: 'module' object has no attribute 'firstEle'
。如何摆脱这个错误?