2013-08-26 28 views
0

我所擁有的是導入另一個模塊element.py的模塊data.pydata.py需要存儲在element.py中的「元素」類,而element.py中的「元素」類是data.py中的模板「元素」類的子類。並且首先運行data.py如何從進口模塊導入導入器

這樣:

data.py

import element.py 

class templateElement(object): 
    # all the class stuff here 

class templateOtherObject(object): 
    # needs element.py's custom element object methods and data 

element.py

import data.py 

class element(data.templateElement): 
    # class stuff here 

那麼,如何得到它在那裏他們可以得到兩個從每個信息其他雖然沒有模板方法分別指定到自己的文件。或者他們必須是?

如何設置模板文件,同時仍然可以在模板文件的不同模板類中使用自定義類?

+0

您應該避免循環導入。將'templateElement'移出到不同的模塊,並從'data'和'element'中導入。 –

+0

請注意,您需要導入'element',* not *'element.py'。 –

+0

[Python中的循環導入依賴關係]的可能重複(http://stackoverflow.com/questions/1556387/circular-import-dependency-in-python) –

回答

0

如何:

data.py

class templateElement(object): 
    # all the class stuff here 

def somefunction(): 
    from element import element 
    # needs element.py's element object 

意思,只是等待,直到你不得不使用它。這樣加載data.py不會導致循環導入。

+0

所以你只是說要導入元素的必要部分。 py文件? – Conflagrationator

+0

另外,我怎樣才能做到這一點,因爲我導入element.py文件的方式是通過動態包裝並通過字符串加載。自定義元素類具有相同的名稱。 – Conflagrationator

+0

'exec(「來自元素導入元素」)'?或者我不明白你的問題 –