193

I've been developing a Django web application deployed on an Apache server with WSGI, and everything has been going smoothly. Today, I made some minor changes to my app's admin.py in an attempt to customize the build-in Django Admin interface, and initially made a syntax error (an unclosed parenthesis). This meant that when I touched wsgi.py and loaded the code (I have WSGI running in daemon mode on my virtual host), my website was replaced with an Internal Server Error because WSGI stopped when it hit the syntax error.

So I fixed the syntax error, checked that I didn't have any more with manage.py check, and touched wsgi.py to redeploy. But my website still displays an Internal Server Error! Checking the Apache logs, this is what I see:

[Sun Nov 23 13:52:46 2014] [info] mod_wsgi (pid=19093): Create interpreter 'quotes.cs.cornell.edu|'.
[Sun Nov 23 13:52:46 2014] [info] mod_wsgi (pid=19093): Adding '/extra/www/html/quotes/quotes_django' to path.
[Sun Nov 23 13:52:46 2014] [info] mod_wsgi (pid=19093): Adding '/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/' to path.
[Sun Nov 23 13:52:46 2014] [info] [client 128.84.33.19] mod_wsgi (pid=19093, process='quotes.cs.cornell.edu',
  application='quotes.cs.cornell.edu|'): Loading WSGI script '/extra/www/html/quotes/quotes_django/quotes_django/
wsgi.py'.
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] mod_wsgi (pid=19093): Target WSGI script '/extra/www/html/
quotes/quotes_django/quotes_django/wsgi.py' cannot be loaded as Python module.
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] mod_wsgi (pid=19093): Exception occurred processing WSGI
script '/extra/www/html/quotes/quotes_django/quotes_django/wsgi.py'.
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] Traceback (most recent call last):
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/extra/www/html/quotes/quotes_django/
quotes_django/wsgi.py", line 14, in <module>
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     application = get_wsgi_application()
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     django.setup()
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/__init__.py", line 21, in setup
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     apps.populate(settings.INSTALLED_APPS)
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/apps/registry.py", line 115, in populate
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     app_config.ready()
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/contrib/admin/apps.py", line 22, in ready
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     self.module.autodiscover()
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/contrib/admin/__init__.py", line 23, in autodiscover
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     autodiscover_modules('admin', register_to=site)
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/utils/module_loading.py", line 74, in autodiscover_modules
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     import_module('%s.%s' % (app_config.name,         
module_to_search))
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/usr/lib64/python2.7/importlib/__init__.py", line 
37, in import_module
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     __import__(name)
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]   File "/extra/www/html/quotes/quotes_django/quotespage/
admin.py", line 25
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]     approve_quotes.short_description = "Approve selected
quotes"
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]                  ^
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] SyntaxError: invalid syntax
[Sun Nov 23 13:53:36 2014] [info] [client 128.84.33.19] mod_wsgi (pid=19093, process='quotes.cs.cornell.edu',
  application='quotes.cs.cornell.edu|'): Loading WSGI script '/extra/www/html/quotes/quotes_django/quotes_django/
wsgi.py'.
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] mod_wsgi (pid=19093): Target WSGI script '/extra/www/html/
quotes/quotes_django/quotes_django/wsgi.py' cannot be loaded as Python module.
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] mod_wsgi (pid=19093): Exception occurred processing WSGI
script '/extra/www/html/quotes/quotes_django/quotes_django/wsgi.py'.
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] Traceback (most recent call last):
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19]   File "/extra/www/html/quotes/quotes_django/         
quotes_django/wsgi.py", line 14, in <module>
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19]     application = get_wsgi_application()
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19]     django.setup()
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/__init__.py", line 21, in setup
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19]     apps.populate(settings.INSTALLED_APPS)
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-
packages/django/apps/registry.py", line 78, in populate
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19]     raise RuntimeError("populate() isn't reentrant")
[Sun Nov 23 13:53:36 2014] [error] [client 128.84.33.19] RuntimeError: populate() isn't reentrant

The first series of errors shows WSGI failing due to the syntax error in my admin.py. However, the second series of errors seems to show an error internal to Django:

RuntimeError: populate() isn't reentrant

thrown from the populate method of registry.py.

Googling this error message returns surprisingly little information, none of it from Django documentation. Apparently, it can sometimes happen if you name an app twice in your settings.py, but I'm not doing that. More importantly, I haven't changed settings.py since the point where the website was working fine -- the only thing I changed was admin.py.

I tried reverting all the changes I made, so all my Python code is back in the state it was when the website was working -- and I still get the populate() isn't reentrant error when I try to make WSGI reload the code!

I've also tried commenting-out different apps in the INSTALLED_APPS section of settings.py, and even with only 'django.contrib.staticfiles' enabled the error still happens. Weirdly, I still get the error even if I comment out all the apps -- Django throws the error even when it isn't loading any apps!

Does anyone know what's going on here? Or any better way for me to debug this error, since the traceback in the Apache log is pretty unhelpful?

Notes: I'm using Django 1.7, Apache 2.2, and Python 2.7.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
Edward
  • 5,942
  • 4
  • 38
  • 55
  • 2
    I'd try removing all .pyc files that might be around. – dukebody Nov 23 '14 at 20:24
  • Nope, removing all .pyc files didn't help. Touching `wsgi.py` results in the same Apache error, and the .pyc files aren't recreated. – Edward Nov 23 '14 at 20:28
  • 1
    Have you tried restarting Apache? – dukebody Nov 23 '14 at 20:29
  • I can't restart Apache because I don't have the rights to on this server. The administrator with root access won't be back in the office until Monday. – Edward Nov 23 '14 at 20:32
  • In my case it was not installed app from INSTALLED_APPS in environment. – maciek Aug 19 '15 at 11:23
  • I know it's 4 years on but there are a LOT of different answers here. Can you @Edward recall if any solved the problem? – ggdx Feb 14 '18 at 13:00
  • The only two answers that solved the problem for *me* are the one I posted (making wsgi.py kill itself) and the one @seddonym posted (touching an "earlier" file). However, it seems like this error can be caused by more than one kind of problem, so maybe the other answers have worked for other people in different situations. – Edward Feb 21 '18 at 15:27
  • In my case I had upgraded to Django 3 in my requirements file but didn't update psycopg2. Running `pip install --upgrade psycopg2` fixed the problem – User Feb 19 '20 at 09:54
  • @gman Back in 2014 when I asked this question, Django 1.7 and Python 2.7 were still supported. I've long since upgraded to newer versions of both, and I would hope anyone still reading this question has too. – Edward Aug 11 '22 at 20:48

40 Answers40

281

This is caused by a bug in your Django settings somewhere. Unfortunately, Django's hiding the bug behind this generic and un-useful error message.

To reveal the true problem, open django/apps/registry.py and around line 80, replace:

raise RuntimeError("populate() isn't reentrant")

with:

self.app_configs = {}

This will allow Django to continue loading, and reveal the actual error.

I've encountered this error for several different causes. Once was because I had a bad import in one of my app's admin.py.

Cerin
  • 60,957
  • 96
  • 316
  • 522
  • 35
    This is by far the most useful answer in this thread. Now I know -> `django.core.exceptions.ImproperlyConfigured: psycopg2_version 2.5.4 or newer is required; you have 2.5 (dt dec pq3 ext)` – RickyA Jun 18 '19 at 14:00
  • 1
    Looks like this error in general is caused by some long running Django service (such as wsgi daemon process) generating an import error. The first occurrence generates a correct error log including the import error but all subsequent web access would generate this "reentrant" error. It seems that Django tries to reload the failed apps but they are "already imported" for the reentrant check. – Qi Fan Jul 12 '19 at 01:21
  • 4
    Saved my life. This solution allowed me to check successfully on the missing requirements and compatibility errors between them. – Ángel Jiménez Aug 06 '19 at 18:01
  • Is this dangerous to try in a production environment? I'm getting those erros in production, but don't know how to reproduce them in my machine. – luislhl Aug 20 '19 at 18:16
  • 2
    This was perfect. Changing that one line let me see I had a simple import error. I couldn't tell what was going on before that. – James Dec 03 '19 at 21:46
  • 5
    @RickyA and this was by far the most useful comment as I didn't even have to bother with this answer and just tried `pip install --upgrade psycopg2` and that fixed it. – User Feb 19 '20 at 09:52
  • 2
    It should definitely be the right answer, and furthermore this should be updated in Django :) Thanks! – Ori Apr 05 '20 at 07:29
  • 2
    If someone uses django with `docker` (or `docker-compose`), the line can easily be replaced in the `Dockerfile` while creating the image by adding this line: `RUN sed -i "s/raise RuntimeError(\"populate() isn't reentrant\")/self.app_configs = {}/g" /usr/local/lib/python3.7/site-packages/django/apps/registry.py` (the Python version may have to be adapted). – mhellmeier Apr 18 '20 at 23:51
  • This required removing cached compiled registry.pyc and restarting httpd. But it did finally tell me what's wrong! – Maksym Aug 05 '20 at 17:49
  • 1
    I'm very impressed by how much in-depth knowledge of the internals of Django library files this answer demonstrates!! – Menas Jan 09 '21 at 11:35
  • As per [this answer](https://stackoverflow.com/a/43415044/1234438), `python manage.py check` achieves the same without the need to alter django code. – Cornflex May 02 '21 at 21:23
  • 2
    How do I navigate to `django/apps/registry.py` directory. I have installed django using pip. – Jeet Patel Jun 06 '21 at 05:56
  • This was so useful, I logged in to my S/O account from my work laptop which involved me changing my google password, just so I can upvote this. – Vimuth Jun 02 '22 at 01:46
  • Thanks, my issue was resolved. After changing this and restarting apache2, the actual error printed. Mine was an error in connecting to MySQL database (wrong credentials). – NINSIIMA WILBER Feb 04 '23 at 07:54
  • @JeetPatel It is not mentioned where you can find the registry.py . I found mine in the pythonVersion/site-packages/django package. I am using conda for virtual env, so it is located at the following place, `/home/userName/miniconda3/envs/virtualEnvName/lib/python3.11/site-packages/django/apps/registry.py` If you are getting the error in the vscode, simply ctrl+click the link it will navigate you to the file. – Memin Jul 31 '23 at 22:32
72

Update from the Future

Since this question has continued to receive attention years after I originally asked it, I thought I should update my answer to better help future readers solve their problems.

It turns out there are (at least) two different reasons you could be getting the "populate() isn't reentrant" error, and thus two different approaches to solving the problem:

  1. There is an error in your Python code or your Django settings that makes your app fail to initialize correctly. As @Cerin's answer points out, Django hides the real problem behind the unhelpful "populate isn't reentrant" message. To fix this and reveal the actual error, follow @Cerin's advice and edit django/apps/registry.py to make Django stop throwing the RuntimeError.

  2. There was at one point an error in your Python code, but you have fixed it, and Django still keeps failing with this message because WSGI won't reload your fixed code. This is a WSGI problem, not a Django problem. One way to fix it is to temporarily edit wsgi.py so that its application function kills the WSGI process (forcing it to restart), as I described in my original answer; another is to set the startup-timeout option of mod_wsgi so that WSGI will restart itself, as @Graham Dumpleton described in the comments. Restarting the entire Apache server also fixes this problem, because it will incidentally restart WSGI, although that's a bit heavy-handed and not always possible if you're not an admin on the webserver.

Original answer below:


My server's administrator restarted Apache, and that magically fixed this problem. The exact same Python files loaded without causing populate() isn't reentrant. I even tried loading another file with a syntax error, then fixing it, and the server was able to load the new file and run correctly with no problems.

I still don't know what was going wrong, but I'm marking this as answered since the problem is gone. (Well, I'll mark it as answered as soon as StackOverflow allows me to accept my own answer.)

Update: After continuing to get this error when I accidentally upload Python with syntax errors, I figured out a workaround that's easier than restarting Apache. When WSGI starts throwing the populate() isn't reentrant error, I replace my Django project's wsgi.py with this simple function:

def application(environ, start_response):
    if environ['mod_wsgi.process_group'] != '': 
        import signal
        os.kill(os.getpid(), signal.SIGINT)
    return ["killed"]

Then I reload my website, and the WSGI daemon process restarts (which I can tell by looking at the Apache log, even though the website still displays the same 500 error).

If I then change wsgi.py back to normal and reload again, WSGI successfully picks up my code without throwing populate() isn't reentrant (assuming I have no syntax errors this time). So the entirety of Apache doesn't need to restart, just the WSGI process, and I can do that without root privileges.

Edward
  • 5,942
  • 4
  • 38
  • 55
  • 9
    For me the problem was not migrating changes I had made to a model in one of my apps. – user2662692 Aug 12 '15 at 17:40
  • Ok, I am facing almost the same problem and yes it gets fixed on rebooting the machine and restarting the server. But the problem keeps repeating. Sometimes all of a sudden. within a month it has occured thrice. If anybody has any idea, please help. – Rohit Apr 04 '16 at 16:05
  • @Rohit, if you want help with your situation, you should probably post a new question describing it. You're unlikely to get an answer in the comments here. – Edward Apr 04 '16 at 16:23
  • 4
    There is a ``startup-timeout`` option for daemon mode of mod_wsgi in more recent versions of mod_wsgi which assists in recovering from transient errors when Django is being initialised, such as a database not being available. The timeout will cause the process to be automatically restarted if WSGI application doesn't load properly after the timeout period. Even that will not help where you have a permanent issue with your own code. In that case, look for the very first error, not the ``populate()`` error as it will give the real reason your code fails. – Graham Dumpleton Oct 16 '16 at 00:52
  • 1
    What do you mean "Django project's wsgi.py with this simple function"? How do you replace a file with a function? – Cerin Oct 27 '17 at 05:25
  • @Cerin: That means the contents of the file became equal to the function I listed. I replaced a Python file with a different Python file, and the new Python file contained only one function. – Edward Oct 27 '17 at 19:01
  • 1
    Well, this accepted solution might have worked for you, but I wouldn't regard it as accepted, as this is quite an ugly fix. To those that may face this problem, for me the reason was an error in the python code (in more detail, I upgraded django and a third party package was breaking and had to update this package as well. But this error went unnoticed and was raised during app initialization which then caused apps to reload, hence the exception). I also had this in the past, and again there was some error during the load process. So debugging your app upon initialize is a good place to start. – babis21 Mar 09 '20 at 10:39
  • There are actually two different problems here, and two different kinds of solutions. One problem is that there is an error in your Python code that makes your app fail to initialize correctly, and then as @Cerin's answer points out, Django "hides" the real problem behind the unhelpful "populate isn't re-entrant" message. The other problem is that, even once you fix the error in your code, Django will *still* keep failing with this message because WSGI won't reload your fixed code until you forcibly kill it. My solution fixes the latter problem. – Edward Mar 11 '20 at 20:23
  • In my case, my database server was shut down. I just had to restart it. – Bobort Mar 31 '23 at 14:04
64

I know this is an old answer but I will contribute with my solution:

As a way to diagnose the source of the problem run manage.py checkand see if you find anything there

In my case an outdated requirement was the issue and django was failing to import a submodule

Make sure that your requirements are up to date

Jesus Gomez
  • 1,460
  • 13
  • 30
  • 2
    I get Error 500 on Apache and I can't found the problem. By this command `manage.py check` I found the problem. Thank you. – Ali Hesari Sep 13 '17 at 22:17
  • Great, much prefer this over uncommenting lines of django code! Thanks! – Cornflex May 02 '21 at 21:21
  • I get Error 500 on IIS, by manage.py check command, found the problem. Some new packages missing. Thanks very much. – danny May 24 '21 at 03:20
  • 1
    using manage.py check actually raised RuntimeError: populate() isn't reentrant for me.. – Phil Jun 25 '21 at 20:50
20

It's not a response but a reflexion.

When you upgrade to django 1.7 and you have a 500 error and reload your page, Apache says "populate() isn't reentrant". I think it's when you load your page, Apache load all the modules you need for your app and when the error is handle it doesn't unload module. So, when you reload your page, apache load again theses modules but it's already loaded. So, apache says "populate() isn't reentrant".

I've two actions to correct this : Restart apache, or correct the error that handle the first 5OO error.

Try restarting apache with:

sudo service httpd restart

I hope it will help you.

Edgar Ortega
  • 1,672
  • 18
  • 22
10

If you're getting this error when using Google App Engine check your logs for other errors which might be causing this. I was getting:

ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named _sqlite3

You can't use SQLite with Google App Engine so commenting out the DATABASES section of settings.py stopped that error and the RuntimeError("populate() isn't reentrant") error as well.

donturner
  • 17,867
  • 8
  • 59
  • 81
  • This explains why everything worked fine locally but got 500 error when deployed to GAE. A small hint: searching text "raise" on GAE log page generated exact one hit which led to the error message pointed out by donturner. His solution saved my day :-) – Golden Thumb Feb 01 '18 at 13:36
4

You may be able to fix it without restarting Apache by touching a file (other than wsgi.py) that is early on in the loading process. For example, your settings file:

$ touch settings.py

I haven't properly solved this either, but more info in my question here: Code change monitoring malfunctioning with Django 1.7 on mod-wsgi

Community
  • 1
  • 1
seddonym
  • 16,304
  • 6
  • 66
  • 71
3

Apache stores the wsgi file in its cache. Disable Apache caching of python files

So first delete the wsgi file and restart your acpache and then add wsgi file again and restart the apache.

Community
  • 1
  • 1
Abhishek Lodha
  • 737
  • 2
  • 7
  • 30
3

This error also generated if inconsistence use of space and tab in the code.

webbyfox
  • 1,049
  • 10
  • 22
2

I experienced this same problem, and the source of the error for me was just a syntax error in a file I was working with. After fixing the typo, the populate() is not reentrant error disappeared.

If you are running django from a wsgi script, you might be able to identify the typo by just running the wsgi script from the command line. For example:

cd /usr/local/www/wsgi-scripts/
python djangolauncher.wsgi
Peter
  • 1,312
  • 3
  • 15
  • 16
2

I just faced the same problem so I started looking around.

Now I've got it working, so I thought I should share it with you guys!

All I did was do chown user:group /to/path -R and chmod 770 /to/path -R all over again and it worked.

Behzad
  • 89
  • 7
2

This looks like a nice collection of valid responses for the same Apache mod-wsgi error, each guy posting the one that works for him/her, so here is mine:

Do not forget to update your project requirements after deploying :)

José L. Patiño
  • 3,683
  • 2
  • 29
  • 28
2

If you tested your syntax in your dev environment and everything is ok, then the problem is here

WSGIDaemonProcess celeryEnv python-path=/var/www/celeryEnv/lib/python3.6/site-packages user=apache group=apache python-home=/var/www/celeryEnv

apache only look at

python-path=/var/www/celeryEnv/lib/python3.6/site-packages

so any modules in lib64 will not be recognized in apache

I found a workaround by coping all modules in /var/www/celeryEnv/lib64 to /var/www/celeryEnv/lib

now apache will work here's the line from my own servers

WSGIDaemonProcess domain.com python-path=/home/user/app/env/lib/python3.6/site-packages:/home/user/app

Note: there's beneficial information already here, but I put this answer because My servers are mostly Centos-7 which are working as it should with my answer !!

1

Setup: Ubuntu 14.04, Django 1.10, Python 3.5 (in virtualenv).

I tried many of these solutions without luck, but then I noticed that the Apache error log contains two different errors in my case. One that happens when someone tries to visit a page, another that happens at start up. I missed the startup one because I usually tried to refresh the page a couple of times and thus only saw the on visit error repeated a few times.

I then searched for solutions to the startup error instead and the solution to this question worked for me. Briefly, it involves updating the mod_wsgi package in a roundabout way.

I had been receiving warnings for months about the mismatch in mod_wsgi versions, but suddenly it resulted in Apache error 500's. Doesn't make any sense to me.

My guess is that this RuntimeError: populate() isn't reentrant error is usually a sign that one should look for a startup error, which indicates the real problem.

on visit

[Sat Oct 15 03:38:08.900966 2016] [:error] [pid 28272] [remote 95.166.81.114:39651] mod_wsgi (pid=28272): Target WSGI script '/django/GP/GP/wsgi.py' cannot be loaded as Python module.
[Sat Oct 15 03:38:08.901409 2016] [:error] [pid 28272] [remote 95.166.81.114:39651] mod_wsgi (pid=28272): Exception occurred processing WSGI script '/django/GP/GP/wsgi.py'.
[Sat Oct 15 03:38:08.901662 2016] [:error] [pid 28272] [remote 95.166.81.114:39651] Traceback (most recent call last):
[Sat Oct 15 03:38:08.902184 2016] [:error] [pid 28272] [remote 95.166.81.114:39651]   File "/django/GP/GP/wsgi.py", line 16, in <module>
[Sat Oct 15 03:38:08.902217 2016] [:error] [pid 28272] [remote 95.166.81.114:39651]     application = get_wsgi_application()
[Sat Oct 15 03:38:08.902501 2016] [:error] [pid 28272] [remote 95.166.81.114:39651]   File "/django/env/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[Sat Oct 15 03:38:08.902529 2016] [:error] [pid 28272] [remote 95.166.81.114:39651]     django.setup(set_prefix=False)
[Sat Oct 15 03:38:08.902726 2016] [:error] [pid 28272] [remote 95.166.81.114:39651]   File "/django/env/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
[Sat Oct 15 03:38:08.902755 2016] [:error] [pid 28272] [remote 95.166.81.114:39651]     apps.populate(settings.INSTALLED_APPS)
[Sat Oct 15 03:38:08.902924 2016] [:error] [pid 28272] [remote 95.166.81.114:39651]   File "/django/env/lib/python3.5/site-packages/django/apps/registry.py", line 78, in populate
[Sat Oct 15 03:38:08.902953 2016] [:error] [pid 28272] [remote 95.166.81.114:39651]     raise RuntimeError("populate() isn't reentrant")
[Sat Oct 15 03:38:08.903111 2016] [:error] [pid 28272] [remote 95.166.81.114:39651] RuntimeError: populate() isn't reentrant

start up

[Sat Oct 15 03:38:08.900966 2016] [:error] [pid 28272] [remote 95.166.81.114:39651] mod_wsgi (pid=28272): Target WSGI script '/django/GP/GP/wsgi.py' cannot be loaded as Python module.
[Sat Oct 15 03:38:08.901409 2016] [:error] [pid 28272] [remote 95.166.81.114:39651] mod_wsgi (pid=28272): Exception occurred processing WSGI script '/django/GP/GP/wsgi.py'.
[Sat Oct 15 03:38:08.901662 2016] [:error] [pid 28272] [remote 95.166.81.114:39651] Traceback (most recent call last):
[Sat Oct 15 03:38:08.902184 2016] [:error] [pid 28272] [remote 95.166.81.114:39651]   File "/django/GP/GP/wsgi.py", line 16, in <module>
[Sat Oct 15 03:38:08.902217 2016] [:error] [pid 28272] [remote 95.166.81.114:39651]     application = get_wsgi_application()
[Sat Oct 15 03:38:08.902501 2016] [:error] [pid 28272] [remote 95.166.81.114:39651]   File "/django/env/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[Sat Oct 15 03:38:08.902529 2016] [:error] [pid 28272] [remote 95.166.81.114:39651]     django.setup(set_prefix=False)
[Sat Oct 15 03:38:08.902726 2016] [:error] [pid 28272] [remote 95.166.81.114:39651]   File "/django/env/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
[Sat Oct 15 03:38:08.902755 2016] [:error] [pid 28272] [remote 95.166.81.114:39651]     apps.populate(settings.INSTALLED_APPS)
[Sat Oct 15 03:38:08.902924 2016] [:error] [pid 28272] [remote 95.166.81.114:39651]   File "/django/env/lib/python3.5/site-packages/django/apps/registry.py", line 78, in populate
[Sat Oct 15 03:38:08.902953 2016] [:error] [pid 28272] [remote 95.166.81.114:39651]     raise RuntimeError("populate() isn't reentrant")
[Sat Oct 15 03:38:08.903111 2016] [:error] [pid 28272] [remote 95.166.81.114:39651] RuntimeError: populate() isn't reentrant
[Sat Oct 15 03:38:43.291502 2016] [:error] [pid 28272] Exception ignored in: <module 'threading' from '/usr/lib/python3.4/threading.py'>
[Sat Oct 15 03:38:43.291579 2016] [:error] [pid 28272] Traceback (most recent call last):
[Sat Oct 15 03:38:43.291604 2016] [:error] [pid 28272]   File "/usr/lib/python3.4/threading.py", line 1288, in _shutdown
[Sat Oct 15 03:38:43.292356 2016] [:error] [pid 28272]     assert tlock is not None
[Sat Oct 15 03:38:43.292377 2016] [:error] [pid 28272] AssertionError: 
[Fri Oct 14 23:38:43.412942 2016] [:error] [pid 28299] Exception ignored in: <module 'threading' from '/usr/lib/python3.4/threading.py'>
[Fri Oct 14 23:38:43.413044 2016] [:error] [pid 28299] Traceback (most recent call last):
[Fri Oct 14 23:38:43.413076 2016] [:error] [pid 28299]   File "/usr/lib/python3.4/threading.py", line 1288, in _shutdown
[Fri Oct 14 23:38:43.425037 2016] [:error] [pid 28275] Exception ignored in: <module 'threading' from '/usr/lib/python3.4/threading.py'>
[Fri Oct 14 23:38:43.425125 2016] [:error] [pid 28275] Traceback (most recent call last):
[Fri Oct 14 23:38:43.425157 2016] [:error] [pid 28275]   File "/usr/lib/python3.4/threading.py", line 1288, in _shutdown
[Fri Oct 14 23:38:43.427625 2016] [:error] [pid 28274] Exception ignored in: <module 'threading' from '/usr/lib/python3.4/threading.py'>
[Fri Oct 14 23:38:43.427694 2016] [:error] [pid 28274] Traceback (most recent call last):
[Fri Oct 14 23:38:43.427722 2016] [:error] [pid 28274]   File "/usr/lib/python3.4/threading.py", line 1288, in _shutdown
[Fri Oct 14 23:38:43.432020 2016] [:error] [pid 28273] Exception ignored in: <module 'threading' from '/usr/lib/python3.4/threading.py'>
[Fri Oct 14 23:38:43.432078 2016] [:error] [pid 28273] Traceback (most recent call last):
[Fri Oct 14 23:38:43.432105 2016] [:error] [pid 28273]   File "/usr/lib/python3.4/threading.py", line 1288, in _shutdown
[Fri Oct 14 23:38:43.438577 2016] [:error] [pid 28299]     assert tlock is not None
[Fri Oct 14 23:38:43.438654 2016] [:error] [pid 28299] AssertionError: 
[Fri Oct 14 23:38:43.442174 2016] [:error] [pid 28274]     assert tlock is not None
[Fri Oct 14 23:38:43.442226 2016] [:error] [pid 28274] AssertionError: 
[Fri Oct 14 23:38:43.447227 2016] [:error] [pid 28276] Exception ignored in: <module 'threading' from '/usr/lib/python3.4/threading.py'>
[Fri Oct 14 23:38:43.447294 2016] [:error] [pid 28276] Traceback (most recent call last):
[Fri Oct 14 23:38:43.447326 2016] [:error] [pid 28276]   File "/usr/lib/python3.4/threading.py", line 1288, in _shutdown
[Fri Oct 14 23:38:43.448813 2016] [:error] [pid 28277] Exception ignored in: <module 'threading' from '/usr/lib/python3.4/threading.py'>
[Fri Oct 14 23:38:43.448876 2016] [:error] [pid 28277] Traceback (most recent call last):
[Fri Oct 14 23:38:43.448903 2016] [:error] [pid 28277]   File "/usr/lib/python3.4/threading.py", line 1288, in _shutdown
[Fri Oct 14 23:38:43.450188 2016] [:error] [pid 28273]     assert tlock is not None
[Fri Oct 14 23:38:43.450231 2016] [:error] [pid 28273] AssertionError: 
[Fri Oct 14 23:38:43.456680 2016] [:error] [pid 28275]     assert tlock is not None
[Fri Oct 14 23:38:43.456737 2016] [:error] [pid 28275] AssertionError: 
[Fri Oct 14 23:38:43.461761 2016] [:error] [pid 28277]     assert tlock is not None
[Fri Oct 14 23:38:43.461826 2016] [:error] [pid 28277] AssertionError: 
[Fri Oct 14 23:38:43.466165 2016] [:error] [pid 28276]     assert tlock is not None
[Fri Oct 14 23:38:43.466219 2016] [:error] [pid 28276] AssertionError: 
[Fri Oct 14 23:38:43.658971 2016] [mpm_prefork:notice] [pid 28268] AH00169: caught SIGTERM, shutting down
[Sat Oct 15 03:38:43.691909 2016] [:error] [pid 28272] Exception ignored in: <module 'threading' from '/usr/lib/python3.4/threading.py'>
[Sat Oct 15 03:38:43.691968 2016] [:error] [pid 28272] Traceback (most recent call last):
[Sat Oct 15 03:38:43.691996 2016] [:error] [pid 28272]   File "/usr/lib/python3.4/threading.py", line 1288, in _shutdown
[Sat Oct 15 03:38:43.693126 2016] [:error] [pid 28272]     assert tlock is not None
[Sat Oct 15 03:38:43.693159 2016] [:error] [pid 28272] AssertionError: 
[Fri Oct 14 23:38:44.490316 2016] [:warn] [pid 28349] mod_wsgi: Compiled for Python/3.4.0.
[Fri Oct 14 23:38:44.490407 2016] [:warn] [pid 28349] mod_wsgi: Runtime using Python/3.4.3.
[Fri Oct 14 23:38:44.505672 2016] [mpm_prefork:notice] [pid 28349] AH00163: Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.19 mod_wsgi/3.4 Python/3.4.3 configured -- resuming normal operations
[Fri Oct 14 23:38:44.505764 2016] [core:notice] [pid 28349] AH00094: Command line: '/usr/sbin/apache2'
Community
  • 1
  • 1
CoderGuy123
  • 6,219
  • 5
  • 59
  • 89
  • Create a new question. Don't ask question as answers. – Graham Dumpleton Oct 15 '16 at 23:08
  • 1
    I'm not asking a question. Posting a solution to the same problem that worked for me. Apache throws error 500 and the error in the log file is the same as the one in the question asked here. Please reread my answer. :) – CoderGuy123 Oct 15 '16 at 23:14
  • Then include the actual solution in your answer rather than link out to another question. The problem is though that that other post doesn't mention the ``populate()`` issue at all and to me looks like an entirely different problem, so I can't see how you think it was solving the same issue. So as far as anyone can tell, you had a different problem which you should have asked a separate question for at the outset rather than confusing the responses here for what looks like a different problem based on details in that other post.. – Graham Dumpleton Oct 16 '16 at 00:48
  • It's unnecessary to repeat the steps here. The errors appear in the same log, just that OP here didn't include all of it. One error is shown when the page is accessed (that's the part in OP here), another when Apache starts (that's the part in the other thread). – CoderGuy123 Oct 16 '16 at 08:13
1

I know it has been a while since this question was asked, but I just ran into this issue due to a problem I haven't seen discussed here. I was getting the RuntimeError: populate() isn't reentrant error due to SELinux on CentOS 7. I had Django served out of a home directory, and I simply had to enable the SELinux boolean that allowed reading home directories, as the populate() error was due to a permissions issue. The solution for me was setsebool -P httpd_read_user_content 1. I hope this helps someone having this issue.

Ad Astra
  • 599
  • 1
  • 4
  • 10
  • We had the same problem with CentOS 7 and SELinux. We fixed it by using `chcon` to change the context of the problem `.so` file to `httpd_sys_script_exec_t `. – Jon Dec 19 '18 at 15:17
1

In my case the error occured because a required pip-package was missing.

So I did a pip install -r requirements.txt, restarted apache and things worked again.

flix
  • 1,821
  • 18
  • 23
1

In my case, I had a circular import, which cause an error that break the populate method.

1

The multitude of answers makes it clear; this is a generic error that can have multiple root causes, typically related to loading Apache/WSGI.

All of these answers on this page should function as a kind of checklist, and in that vein I want to add the root cause of my instance of this error: failure to add an 'import os' to your settings.py file.

Specifically, we had a developer on our team who intended to remove an unneeded package, and instead removed 'import os' from the top of the production settings.py file. After an apache restart, our application wouldn't restart and we received the dreaded 'RuntimeError: populate() isn't reentrant' error.

A quick 'python manage.py check' did not reveal the issue, but a 'python settings.py' did; the os package was not loaded.

If you have this error, focus your search on checking your settings.py file(s) and also your WSGI file.

Tom Halpin
  • 27
  • 2
  • 3
    More importantly, ensure you go back in the error log and look for the very first error message that occurs after a restart of one of the Apache or mod_wsgi daemon processes. That first error will tell you the true reason for the error, any requests after that for the same process will mention the ``reentrant`` error only. In daemon mode, also ensure you are using ``startup-timeout`` option if you have this problem due to transient errors, rather than coding errors. At least that way it can automatically recover. – Graham Dumpleton Apr 09 '18 at 21:03
  • This is the right answer, the error is generic, check the message just before the runtimeerror. – Popieluch Feb 01 '19 at 20:24
1

RuntimeError: populate() isn't reentrant

Can be anything, that is why there are so many different answers for this question.

The trick is to look at the error message just before the RuntimeError. In your case there seems to be a syntax error in the file /extra/www/htmlquotes/quotes_django/quotespage/admin.py on line 15, see:

Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] File "/extra/www/htmlquotes/quotes_django/quotespage/admin.py", line 25
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] pprove_quotes.short_description = "Approve selected quotes"
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19]                  ^
[Sun Nov 23 13:52:46 2014] [error] [client 128.84.33.19] SyntaxError: invalid syntax
Popieluch
  • 180
  • 1
  • 8
1

I had a recursive django.setup(), eg I tried to write a django.setup() inside a an app/models.py, in the stack trace django tried to point this out near:

... "site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
...
... ./myproject/myapp/models.py ...

so yeah, be sure not to try to setup django while django is being setup...

ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
  • Is there another option for this? I'm using some Celery tasks and would like access to the models so If I add `django.setup()`, the tasks works but I get `Populate isn't reentrant`. – DjangoDev1 Mar 16 '21 at 01:45
  • are you using django-celery specifically? (https://docs.celeryproject.org/en/latest/django/first-steps-with-django.html) I think part of the move to the `@shared_task` decorator is to make celery init play nice with django init – ThorSummoner Mar 16 '21 at 06:15
  • I've been following a lot of tutorials and I only have one `tasks.py` file in which I keep all of the tasks. I've posted a new question about this. - https://stackoverflow.com/questions/66648110/cant-run-celery-task-in-django-i-either-get-appregistrynotready-apps-arent – DjangoDev1 Mar 16 '21 at 12:16
1

Note on AWS Elastic Beanstalk: The default settings.py that is written by Django-admin includes a reference to a local sqlite database as the data source. This will likely work on your local OS, but not on AWS EB, and will give the populate() isn't reentrant runtime error. To test for this, simply comment out the DATABASES={<...>} statement in settings.py, deploy, and re-open the application.

Lord Elrond
  • 13,430
  • 7
  • 40
  • 80
  • This occurs e.g. if you deploy to AWS EB using Django version 2.2 (or later) with the default sqlite database. You'll get `django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).` followed by the `RuntimeError: populate() isn't reentrant`. From the [AWS docs](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html#python-django-setup-venv): "Django 2.2 is incompatible with the Elastic Beanstalk Python 3.6 platform." (at the time of writing) – djvg Mar 05 '20 at 11:32
1

For me the error was a missing mysqlclient package in the requirements.txt file.

First I installed the mysqlclient package with:

pip install mysqlclient

then I updated the requirements.txt file with:

pip freeze > requirements.txt

and this solved my problem.

Julian
  • 886
  • 10
  • 21
1

RuntimeError: populate() isn't reentrant

This Django error is mostly course by wrong import and in most cases, you are trying to import a module twice

0

I had this problem and couldn't find any answer why until I backtracked my commits. Apparently I had added an accidental import, because of auto-completion, that screwed up the setup.

# found in models.py from msilib.schema import SelfReg

In apache error log: RuntimeError("populate() isn't reentrant")

It worked fine in my windows dev environment but failed on the ubuntu/apache server.

Jan
  • 791
  • 1
  • 5
  • 8
0

I was going into this same error after having change the order of this setting :

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

putting it back on the order here before and restarting apache fixed the issue.

Romain Jouin
  • 4,448
  • 3
  • 49
  • 79
0

In my case, I had a custom renderer class for Django Rest Framework, for some purpose I had to override the renderer class's method "get_context" (full disclosure : to make django toolbar give correct SQL query count)

I removed that class and redeployed. It Worked.

Karan Kumar
  • 2,655
  • 19
  • 36
0

Removing the virtualenv directory, recreating the virtualenv, then reinstalling all the requirements fixed it for me.

Farid El Nasire
  • 1,753
  • 4
  • 15
  • 17
0

I hesitate to add this as an answer since it's really just a description of a related case. However, it didn't seem legible as a comment.

I ran into this error when creating a function that could access the Django database without the server running. Mostly, this was being used for inserting test data entries.

My project had the following in its project/apps/app_one/functions.py file:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Project.settings")
import django
django.setup()

This allows access to the Django database through Python scripts without a server running. See here for more info on this approach: https://stackoverflow.com/a/26875729/4573162

I was also keeping basic app functions in this file that didn't require database access and were to be used while a server was running. For example, in my app_one.models.py I might have the following usage of a functions.arg_format function, imported from the app_name/functions.py file:

def save(self, *args, **kwargs):
    self.some_arg = functions.arg_format(self.some_arg)
    super().save(*args, **kwargs)

What this amounts to is that my application, while running, was trying to again run the Django setup().

I dont' know how related this is but it threw the same RuntimeError: populate() isn't reentrant error until I created a separate file for all the database-accessing functions. After the additional setup() import was sidestepped, migrations were able to be completed as expected.

alphazwest
  • 3,483
  • 1
  • 27
  • 39
0

Adding my reason to the list. For me it was because I had a django service named with the same directory as a process directory. Renaming the process/dir fixed the issue.

Mark
  • 587
  • 1
  • 7
  • 13
0

Restarting the Apache server for me solved the problem. You can do that by using the command $ sudo service apache2 restart

Shivam Kohli
  • 449
  • 4
  • 6
0

I had this same issue, what worked for me was commenting out the default database settings in / settings.py . I also read that later versions of django are not compatible with ebs

0

In apps.py, i have set different name from my app name.

Darwin
  • 1,695
  • 1
  • 19
  • 29
0

For me, the problem was the version of SQLite. I switched to MySQL and an everything worked just fine. PS: I'm using a shared hosting from Namecheap. So to install mysqlclient I had to contact the support.

AnwarMEQOR
  • 51
  • 4
0

To see the actual error run the devlopment server:

python manage.py shell

after resolving all the issues, restart your server

sudo service httpd restart
Roshan Summun
  • 571
  • 1
  • 4
  • 9
0

I ran into same issue but in a different way, I was trying to run tests for my django app which caused the same error, upon googling I came to this answer, I see everybody got same error in different environments. I would like to add my answer which cause the problem and solution.

  1. I have added following lines to load the django settings before running tests

import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "admin.settings")
django.setup()

I have added above lines in my tests.py file which helped me to run the tests. but then I have to ran these tests in AzureDevops then I added same lines in my admin.py file, after that I started to see this error enter image description here

removing the lines from admin.py fixed the issue. Hope this helps someone.

-1

To throw in my 2 Euro cents:

I recreated a working setup in Docker. The new Docker setup failed with

populate isn't reentrant

which seems to be a generic error. In my case, I overlooked that

pip install Django

installs the latest version (2.0), instead of the required version 1.11. Changing this to

pip install Django==1.11

fixed my problem.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
-1

I think this is a generic error when something is wrong with settings.py. Sometimes I can find the problem by trial and error by removing the installed apps one at a time. In some cases, its not related to the installed apps. But from my experience, in all cases its a problem within the settings.py file.

Noel Pure
  • 422
  • 2
  • 13
-1

check if you have mentioned your api names twice in the settings.py ' s installed app section.

Application definition

INSTALLED_APPS = [
    ...
    'rest_framework',
    'myapp_api',
    'myapp_api.apps.myappWebserviceApiConfig',    
]

Application definition

INSTALLED_APPS = [
    ...
    'rest_framework',
    'myapp_api.apps.myappWebserviceApiConfig',
]

Removing duplicate entries solved my problem

Rajesh Pandya
  • 1,540
  • 4
  • 18
  • 31
-1

I recently had the same error on my django project [Django1.11].

The problem was the fact that my apps no longer live in a non-standard django location, normally the apps live in the root project dir

I recently moved my apps to an apps folder for logging purposes, butt i forgot to update my templates.

My structure [./awesome_potatoes/apps/first_app/]

So when I start a new app, what happens is django does not prepend the dir name in the apps.py file or any python file for that matter because of the outdated template

apps.py

name = 'first_app'

Fix [after updated my templates]

name = 'apps.first_app'

Illegal Operator
  • 656
  • 6
  • 14
-2

I solved problem with adding __init__.py to my apps folder.

touch /mainprojectfolder/projectfolderwhichcontainswsgi.py/apps/__init__.py

Then it worked!

-5

This couse of lack of memory on your PC/Host/VPS, free some memory and all will be fine.

  • 2
    You've posted the same answer to [three](http://stackoverflow.com/questions/31611117/runtimeerror-populate-isnt-reentrant-in-django/32700115#32700115) [questions](http://stackoverflow.com/questions/30954398/django-populate-isnt-reentrant/32700055#32700055). Please tailor each answer to the question. – Undo Sep 21 '15 at 16:26
  • Why the downvotes? This was the correct answer for me. – Babken Vardanyan Jun 19 '18 at 05:49