2011-06-27 34 views
1

我有把所有的使用日誌的軌道模塊,(usage.py)如何解決循環進口

然後我創造了另一個模塊,繪製圖表,(chart.py) 我想跟蹤的人使用我的chart.py,因此,我將usage.py導入到我的圖表中。

一切似乎很酷到這一點。

現在,我想在usage.py中顯示用法圖表,因此,我嘗試將chart.py導入到usage.py中。

Kaboom! 它給了我這個錯誤: -

ImportError:無法導入名稱圖表。

無論如何要解決這個問題?

在此先感謝。

+0

爲什麼不創建第三個文件:'chart_display.py'? –

+0

循環依賴非常糟糕......永遠不要這樣做。 – JBernardo

+1

重複:http://stackoverflow.com/questions/1556387/circular-import-dependency-in-python – warvariuc

回答

3

這些通常通過將共享組件重構爲第三個模塊來解決,並且每個原始模塊都導入該新模塊。

1

什麼,你也可以做的是以下幾點:

# File n°1, toto.py 
from tata import tataClass 

    class totoClass: 
     def __init__(self,): 
      # here I can use data from tataClass 

# File n°2, tata.py 
    def method_using_toto(): 
     from toto import totoClass 
     # here I can use data from totoClass 

因此,你必須要在位置非常小心的包括