Python and Circular imports yet again -



Python and Circular imports yet again -

say have module looking this:

circlejerking/ ├── bar.py ├── foo.py └── __init__.py

bar.py:

from .foo import foo class bar(foo): pass

foo.py:

from .bar import bar class foo(object): my_bar = bar()

__init__.py:

from .foo import foo .bar import bar __all__ = [ 'foo', 'bar' ]

using module fails because of circular import.

in [1]: circlejerking import foo --------------------------------------------------------------------------- importerror traceback (most recent phone call last) <ipython-input-1-2258014c7099> in <module>() ----> 1 circlejerking import foo /home/me/circlejerking/__init__.py in <module>() ----> 1 .foo import foo 2 .bar import bar 3 4 __all__ = [ 5 'foo', /home/me/circlejerking/foo.py in <module>() ----> 1 .bar import bar 2 3 class foo(object): 4 my_bar = bar() /home/me/circlejerking/bar.py in <module>() ----> 1 .foo import foo 2 3 class bar(foo): 4 pass importerror: cannot import name foo

i understand problem , aware, python related circular import problems have been discussed multiple times, can't seem work. 2 goto answers has problem are:

redesign code, holding wrong or import other module need it.

i certain, design okay , yes, sure 2 classes not in fact 1 thing.

importing other class somewhere else not option, because need them right are.

any ideas?

in general solve injection pattern:

this module fine as-is:

from .foo import foo class bar(foo): pass

then other module becomes:

class foo(object): bar = none @classmethod def set_bar(cls, bar): cls.bar = bar

it's whatever uses foo ensure set_bar called appropriately before utilize it.

python import circular-dependency

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -