Ipython笔记本:导入的脚本函数的名称错误

2024-04-23 07:46:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个脚本sources.pynest.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()

在嵌套.py在

^{pr2}$

当我像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

我不明白为什么会这样。你能告诉我这两种情况有什么不同,以及如何解决这个问题吗?在


Tags: nameinfrompyimport脚本sourcemake
1条回答
网友
1楼 · 发布于 2024-04-23 07:46:59

问题是

import something

以及

^{pr2}$

关于名称空间。在

如果有递归导入,最好不要执行“from something import*”或“import something as something”

这里有一个完整的解释:

Circular (or cyclic) imports in Python

相关问题 更多 >