开发者

how to solve a loop import

I have a module that keep tracks of all the usage log, (usage.py)

I then created another module for drawing charts, (chart.py) I wanna keep track of people using my chart.py, thus, I import usage.py into my chart.

Everything seems cool up to this point.

N开发者_运维技巧ow, I wanna display the chart of the usage in usage.py, thus, I tried to import chart.py into usage.py.

Kaboom ! It gives me this error:-

ImportError: cannot import name chart.

Anyway to solve this?

Thanks in advance.


These are usually solved by refactoring shared components into a third module, and each original one imports that new module.


What you also can do is the following:

# 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

You have therefore to be very carefull in the position on the includes

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜