I have a Django app and somewhere in it is a recursive import that is causing problems. Because of the size of the app I'm having a problem pinpointing the cause of the circular import.
I know that the answer is "just don't write circular imports" but the problem is I'm having a hard time figuring out where the circular import is coming from, so ideally a tool that traced the import back to its origin would be ideal.
Does such a tool exist? Barring that, I feel like I am doing everything I can to avoid circular import problems -- moving imports to the bottom of the page if possible, moving them inside of functions rather than having them at the top, etc. but still running into problems. I'm wondering if there are any tips or tricks for avoiding them altogether.
To elaborate a bit...
In Django specifically when it encounters a circular import, sometimes it throws an error but sometimes it passes through silently but results in a situation where certain models or fields just aren't there. Frustratingly, this often happens in one context (say, the WSGI server) and not in another (the shell). So testing in the shell something like this will work:
Foo.objects.filter(bar__name='Test')
but in the web throws the error:
FieldError: Cannot resolve keyword 'bar__name' into field. Choices are: ...
With several fields conspicuously missing.
So it can't be a straightforward problem with the code since it does work in the shell but not via the website.
Some tool that figured out just what was going on would be great. ImportError
is maybe the least helpful exception message ever.
.pyc
too :) (it happened to me several times) – Knownothing