Home » Django » Python » When Gunicorn fails, but runserver works

When Gunicorn fails, but runserver works

Sometimes I am cheating on PHP with Python (PHP knows and it doesn’t appear to mind). I was developing a Django project and it worked perfectly with manage.py runserver but when run through Gunicorn it was failing with an ImportError.

This really got me worked up – as a programmer, I hate when I don’t understand why something would work in one environment but fail in another.

The problem was circular import in Python!

I had two apps in this project: news and comments. News had the following model:

Comments had something like this:

This worked perfectly well with Django’s built in server, but failed when I tried to run it through Gunicorn. For some reason, this kind of circular import just doesn’t work with it! I am still trying to figure out why, but meanwhile, the fix was to reference ForeigKey with a string. Now I don’t have the offending import in comments/models.py and I reference the foreign key like this:

Leave a Reply

Your email address will not be published. Required fields are marked *