我有两个脚本sources.py
and nest.py
。他们是这样的
来源.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 笔记本环境中如果我执行以下操作
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
我很困惑为什么会发生这种情况。您能否告诉我这两种情况有何不同以及如何解决这个问题?
问题是,两者之间是有区别的
import something
and
from something import *
关于命名空间。
如果您有递归导入,最好永远不要执行“from Something import *”或“import some as someotherthing”
您可以在这里得到完整的解释:
Python 中的循环(或循环)导入 https://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)