2
# models/__init__.py

from shared.cache import Cache

class modelA():
    pass
class modelB():
    pass
class modelC():
    pass

# shared/cache.py

class Cache:
    def methodA():
        modelA.SomeStaticMethod()

Basically what I need is to access modelA from inside the Cache class. If I try to import the models from cache.py, I get an error due to a circular reference error.

I know it seems a little bit weird but it's a very specific issue. Is there anyway to do that?

stefanobaldo
  • 1,957
  • 6
  • 27
  • 40
  • Can you give more detail on what exactly you want to achieve? – 101 Jan 13 '15 at 21:16
  • It's sad how badly screwed up imports are in python. I often run into this problem myself, and have found at least 3 different stupid workarounds. – Aran-Fey Jan 13 '15 at 21:22

1 Answers1

1

You would usually restructure your files so that there is no circular reference error.

Simply answering your question, and usually seen as a workaround, you can import Cache on demand, only within the functions of models/__init__.py that make use of it. This may not be possible in this case, especially if Cache is used as a decorator at the module level.

See also this question.

Community
  • 1
  • 1
gg349
  • 21,996
  • 5
  • 54
  • 64