2016-05-31 27 views
-4

我越來越NameError:全局名稱「X」是沒有定義

NameError: global name 'Ontologia' is not defined 

類Ontologia定義上ontologia.py,我也用

import ontologia 

這是行,我「M越來越問題

onto = Ontologia() 

ontologia.py的完整代碼是在這裏:

class Ontologia(object): 

def __init__(self, name, key, left=None, right=None): 
    self.name = name 
    self.key = key 
    self.left = left 
    self.right = right 
+2

嘗試或者(但不能同時)'從ontologia進口Ontologia',或'到= ontologia.Ontologia()'。 – wflynny

+1

請將您的答案放在答案中,以便OP可以接受它,我可以對其進行投票,並且世界可以知道這個問題已經回答。 –

+0

謝謝,它的工作原理。 有沒有辦法讓我導入整個模塊? –

回答

3

Python的import語句與例如Java的import語句有點不同。

可以從模塊導入每個公共名稱。如果你有興趣,documentation of the import statement確切地定義了什麼是「公共名稱」。例如:

from ontologia import * 
onto = Ontologia() # name 'Ontologia' is defined now 

一般不建議使用這種形式,因爲目前還不清楚這名由import語句定義。 Python程序員喜歡明確。

更好的是:

from ontologia import Ontologia 
onto = Ontologia() # name 'Ontologia' is defined now 

或者:

import ontologia 
onto = ontologia.Ontologia() 
相關問題