Ipython notebook:导入脚本函数的名称错误
我有两个脚本,分别叫做 sources.py
和 nest.py
。它们大概是这样的:
sources.py
import numpy as np
from nest import *
def make_source():
#rest of the code
def detect():
Nest = nest()
Nest.fit()
if __name__=='main':
detect()
nest.py
import numpy as np
from sources import *
class nest(object):
def _init_(self):
self.source = make_source()
def fit(self):
#rest of the code
当我在命令行中运行这个脚本,比如输入 python sources.py
,一切都正常。
但是在 Ipython notebook 环境中,如果我这样做:
In [1]: from sources import *
In [2]: detect()
我就会遇到以下错误:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-5-e9c378341590> in <module>()
----> detect()
C:\sources.pyc in detect()
--> 7 Nest = nest()
C:\nest.pyc in _init_()
--> 7 self.source = make_source()
NameError: global name 'make_source' is not defined
我对为什么会发生这种情况感到困惑。你能告诉我这两种情况有什么不同,以及如何解决这个问题吗?
1 个回答
3
其实这里有一个区别,
import something
和
from something import *
在命名空间方面。
如果你有递归导入,最好不要使用“from something import *”或者“import something as someotherthing”。
你可以在这里找到详细的解释: