36

I keep getting this when I try to load my Django application on production . I tried all the stackoverflow answers but nothing has fixed it. Any other ideas. (I'm using Django 1.5.2 and Apache)

 Traceback (most recent call last):
         File "/var/www/thehomeboard/wwwhome/wsgi.py", line 37, in <module>
           application = get_wsgi_application()
         File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
           django.setup()
         File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
           apps.populate(settings.INSTALLED_APPS)
         File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 78, in populate
           raise RuntimeError("populate() isn't reentrant")
       RuntimeError: populate() isn't reentrant
5
  • How are you running Django? Do you see any other errors anywhere?
    – knbk
    Jun 20, 2015 at 13:16
  • What do you mean by how am I running django? I get the errors in the apache log files only.
    – ip.
    Jun 20, 2015 at 13:20
  • What are you using to run the actual wsgi application? mod_wsgi? This error is caused either by an error during populate(), in which case this is the error you get the next time it is run, or, less likely, by improperly run asynchronous code - for example when using gevent without the proper configuration.
    – knbk
    Jun 20, 2015 at 13:38
  • yes I'm using mod_wsgi
    – ip.
    Jun 20, 2015 at 13:39
  • 1
    When I removed all the non-django apps from INSTALLED_APPS I get other problems but the 'populate() isn't reentrant' is no longer there
    – ip.
    Jun 20, 2015 at 13:50

6 Answers 6

36

This RuntimeError first occured for me after upgrading to Django 1.7 (and still is present with Django 1.8). It is usually caused by an Django application which raises an error, but that error is swallowed somehow.

Here's a workaround which works for me. Add it to your wsgi.py and the real error should be logged:

import os
import time
import traceback
import signal
import sys
from django.core.wsgi import get_wsgi_application

try:
    application = get_wsgi_application()
    print 'WSGI without exception'
except Exception:
    print 'handling WSGI exception'
    # Error loading applications
    if 'mod_wsgi' in sys.modules:
        traceback.print_exc()
        os.kill(os.getpid(), signal.SIGINT)
        time.sleep(2.5)

See this thread on modwsgi for more details.

7
  • 1
    This just keeps giving me an error that says Target WSGI script '/home/admusr/project/WebManager/wsgi.py' does not contain WSGI application 'application'.
    – ewok
    Sep 30, 2015 at 17:10
  • Using this solution - I get "NameError: global name 'sys' is not defined" as this snippet is lacking an "import sys" Nov 25, 2015 at 8:32
  • The above code is not the full wsgi.py file. You should have an import sys somewhere on top of the default file already. If not, add it. Nov 25, 2015 at 10:43
  • 2
    Thanks. This worked for me to find actual issue. -:)
    – Waqas Ali
    Aug 21, 2016 at 19:36
  • 1
    This was very helpful. Thank you.
    – Cerin
    Oct 11, 2017 at 0:30
6

In the end the problem that I had was that I tried to run a second Django app and did not have the following defined in my apache config:

WSGIDaemonProcess ...
WSGIProcessGroup ...

Just learned that you can run a single django app without defining them but when its two it produces a conflict.

1
  • This also happened to me today, when I accidentally deployed my Django project developed for Python 2.x to a Python 3.x instance on Webfaction. I did not investigate the details. Just something to check before going down the rabbit hole.
    – guidos
    Feb 23, 2016 at 15:25
3

There will be many reason to causes to populate() isn't reentrant error. If you look at the registry.py in your in django application probably inside this directory
/python2.7/site-packages/django/apps

        # app_config should be pristine, otherwise the code below won't
        # guarantee that the order matches the order in INSTALLED_APPS.
        if self.app_configs:
            raise RuntimeError("populate() isn't reentrant")

As you see in the comment it says app_config should be pristine. Which means if one of configuration is not correct or required library missing it will rise this populate error. I got this error because I have missed sqlite installation. Even as you see there is no mentioning possible causes in the exception. I installed sqlite by this command on debian

pip install pysqlite

It solved my problem. My exception because of missing pysqlite.Your maybe having missing of another required packages or errors in your settings.py

3

For those using AWS Lambda (and optionally using zappa) this can happen when the size of code & dependencies zipped into a deployment package exceeds 250MB after decompression.

Typically the zip may only be 50 MB but may decompress to over 250MB so you may need to manually unzip the deployment package to check it isn't too large when uncompressed.

https://docs.aws.amazon.com/lambda/latest/dg/limits.html

1

Full disclosure - "populate() isn't reentrant" errors can have multiple causes, and checking any recent config or program changes is a very good idea.

However, this error can also occur when Apache is updated, and a module is no longer valid/is corrupted/needs refreshing. This occurred to us on Webfaction after an Apache update (but may happen on any host).

Using the Apache restart script will NOT help this, because the modules remain loaded on the restart. Depending on your system, and whether the mods are cached even when Apache is shut down, this may help.

Fully stop Apache. On Webfaction, that is:

~/webapps/<YOUR WEB APP>/apache2/bin/stop

Wait a few seconds, and then...

~/webapps/<YOUR WEB APP>/apache2/bin/start

That should correct the issue. If your system caches the mods, you may have to flush the cache before starting.

Hope this helps!

Here is the link they gave me (I know the error is different, but we had this happen for the same reason with the populate error as well):

https://statusblog.webfaction.com/2018/05/16/regarding-glibc_private-errors-in-your-python-application/

1

For me this error was caused, because I hadn't correctly split my INSTALLED_APPS for local and production. Meaning in local I was using django-cors-headers and in production I was not. But I accidentally had left django-cors-headers even though I removed it from my production requirements.txt. After delete cors-headers from the installed apps in production the error went away.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.