如何将自定义python变量导入python NOTEBOOK



令人惊讶的是,我发现我无法将自定义python变量导入到ipython NOTEBOOK中。但是,它可以导入到ipython shell。

config.pyA = 100

在python NOTEBOOK中,

import config
config.A
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-54-a03322ffb792> in <module>()
----> 1 config.LAYER
AttributeError: 'module' object has no attribute 'A'

在python SHELL中,

>>> config.A
'100'

同样的测试在ipython中对我有效。

我会制作一个非常简单的python模块,例如test.py,其中有一个声明:

A = 100

尝试同样的事情

import test test.A

我注意到的另一件事是,在test.py中确实没有声明的情况下,ipython的错误将抛出

--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in () ----> 1 test.B

AttributeError: 'module' object has no attribute 'B'

而不参考CCD_ 6。您运行的是什么版本的ipython?我用IPython 4.0.0进行了测试。(此信息在您启动ipython时输出。)

最新更新