2016-04-03 62 views
0

令人驚訝的是,我發現我無法將自定義python變量導入到ipython NOTEBOOK中。但是,它可以被導入到ipython shell。如何將自定義python變量導入python NOTEBOOK?

config.py,A = 100

在蟒蛇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' 

回答

1

同樣的測試在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'

沒有到test.LAYER參考。你運行的是什麼版本的ipython?我使用IPython 4.0.0進行測試。 (當您啓動ipython時輸出此信息。)

+0

不介意'圖層'。我只是忘了把它轉換成其他一些隨機數字,比如'A'。 –

+0

感謝您的確認。這對我來說就夠了。 –