Daemonizing Celery--No module named 'celery' error - django

I'm trying to daemonize Celery 4.3.0 on Ubuntu 18 following the official documentation. This is for a Django project to off-load intensive tasks.
When the server is running the celery.service should make the Celery worker available to process tasks. However, Apache2 won't even run. If I tail the Apache log I see:
[Sun Sep 29 07:42:07.621273 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] File "<frozen importlib._bootstrap>", line 971, in _find_and_load
[Sun Sep 29 07:42:07.621279 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
[Sun Sep 29 07:42:07.621285 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
[Sun Sep 29 07:42:07.621291 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] File "<frozen importlib._bootstrap_external>", line 678, in exec_module
[Sun Sep 29 07:42:07.621297 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[Sun Sep 29 07:42:07.621303 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] File "/var/www/html/examgap/examgap/__init__.py", line 5, in <module>
[Sun Sep 29 07:42:07.621307 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] from .celery import app as celery_app
[Sun Sep 29 07:42:07.621313 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] File "/var/www/html/examgap/examgap/celery.py", line 5, in <module>
[Sun Sep 29 07:42:07.621317 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] from celery import Celery
[Sun Sep 29 07:42:07.621333 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] ModuleNotFoundError: No module named 'celery'
Celery is definitely installed for Python3. I can run the Celery worker for my app standalone using celery -A examgap worker -l warning--just not as a daemon.
I can also import celery if I launch a Python3 shell.
I've looked at several existing SO questions like this one and think I've eliminated some possibilities. For example, the daemon user is ubuntu and this has rwx permissions for my project folder.
Still I think the problem lies with my celery.service file and the command that starts the daemon either not accessing the right folder or using the right permissions.
Here's my /etc/systemd/system/celery.service:
[Unit]
Description=Celery Service
After=network.target
[Service]
Type=forking
User=ubuntu
Group=ubuntu
EnvironmentFile=/etc/conf.d/celery
WorkingDirectory=/opt/celery
ExecStart=/bin/sh -c '${CELERY_BIN} multi start ${CELERYD_NODES} -A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
ExecStop=/bin/sh -c '${CELERY_BIN} multi stopwait ${CELERYD_NODES} --pidfile=${CELERYD_PID_FILE}' ExecReload=/bin/sh -c '${CELERY_BIN} multi restart ${CELERYD_NODES} -A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
[Install]
WantedBy=multi-user.target
Thanks in advance.
Edit: Here's my /etc/conf.d/celery too:
# Name of nodes to start
# here we have a single node
CELERYD_NODES="eg1"
# or we could have three nodes:
#CELERYD_NODES="w1 w2 w3"
# Absolute or relative path to the 'celery' command:
#CELERY_BIN="/usr/local/bin/celery"
#CELERY_BIN="/virtualenvs/def/bin/celery"
#Think I installed Celery only for user ubuntu
CELERY_BIN="/home/ubuntu/.local/bin/celery"
#CELERYD_CHDIR="/var/www/html/examgap/"
# App instance to use
# comment out this line if you don't use an app
CELERY_APP="examgap"
# or fully qualified:
# CELERY_APP="examgap.tasks:app"
# How to call manage.py
CELERYD_MULTI="multi"
# Extra command-line arguments to the worker
CELERYD_OPTS="--time-limit=300 --concurrency=8"
# - %n will be replaced with the first part of the nodename.
# - %I will be replaced with the current child process index
# and is important when using the prefork pool to avoid race conditions.
CELERYD_PID_FILE="/var/run/celery/%n.pid"
CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
CELERYD_LOG_LEVEL="INFO"
Edit 2: Changed the WorkingDirectory to my project directory and now the celery service is running. Yet the ModuleNotFoundError: No module named 'celery' persists.

[Sun Sep 29 07:42:07.621313 2019] [wsgi:error] [pid 2648:tid 140134825535232] [remote 92.4.204.209:55952] File "/var/www/html/examgap/examgap/celery.py", line 5, in <module>
Seems like you have your own celery.py, if so you should rename it to avoid import problems

Here I let some links that look excellent. Just now I'm doing the same.
https://realpython.com/asynchronous-tasks-with-django-and-celery/
(recommended)
http://blog.fixter.org/hospedando-django-celery-rabbitmq-en-aws-parte-1/
(i think it's tricky way)
https://dev.to/idrisrampurawala/deploying-django-with-celery-and-redis-on-ubuntu-3fo6 (level pro)
I hope this work for you.

Related

django.core.exceptions.ImproperlyConfigured: SQLite 3.9.0 or later is required (found 3.7.17) Error in Apache Logs

I'm getting below error in Apache error logs:
django.core.exceptions.ImproperlyConfigured: SQLite 3.9.0 or later is required (found 3.7.17)
I verified the sqlite3 version both in virtual environment and non virtual environment (standard). I can see the latest sqlite3 in both Python.
$ python3.7
Python 3.7.12 (default, Nov 8 2021, 09:02:58)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sqlite3 import dbapi2 as Database
>>> Database.sqlite_version_info
(3, 36, 0)
Error log from Apache web server:
[Mon Nov 08 15:02:33.698244 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] backend = load_backend(db['ENGINE'])
[Mon Nov 08 15:02:33.698249 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] File "/home/rafiq/myprojectenv/lib/python3.7/site-packages/django/db/utils.py", line 111, in load_backend
[Mon Nov 08 15:02:33.698252 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] return import_module('%s.base' % backend_name)
[Mon Nov 08 15:02:33.698257 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
[Mon Nov 08 15:02:33.698260 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] return _bootstrap._gcd_import(name[level:], package, level)
[Mon Nov 08 15:02:33.698265 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] File "/home/rafiq/myprojectenv/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 73, in <module>
[Mon Nov 08 15:02:33.698267 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] check_sqlite_version()
[Mon Nov 08 15:02:33.698273 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] File "/home/rafiq/myprojectenv/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 69, in check_sqlite_version
[Mon Nov 08 15:02:33.698276 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] 'SQLite 3.9.0 or later is required (found %s).' % Database.sqlite_version
[Mon Nov 08 15:02:33.698294 2021] [wsgi:error] [pid 1459] [remote 192.168.0.105:62615] django.core.exceptions.ImproperlyConfigured: SQLite 3.9.0 or later is required (found 3.7.17).
I see the latest version Python but why Apache reports 3.7.17?
I resolved the problem with the below steps.
mv /usr/bin/sqlite3 /usr/bin/sqlite3_3.7.9
ln -s /usr/local/bin/sqlite3 /usr/bin/sqlite3
The document which I followed doesn't contain the above-mentioned two commands and that helped to overcome the actual issue.
try this:
# Intall sqlite3 version 3.39.4
- wget https://www.sqlite.org/2022/sqlite-autoconf-3390400.tar.gz
- tar xvfz sqlite-autoconf-3390400.tar.gz
- cd sqlite-autoconf-3390400
- ./configure
- make
- make install
- PATH=$PATH:/usr/local/lib
- sqlite3 --version

Ubuntu / Django / Logging / PermissionError: [Errno 13] Permission denied

Ubuntu 18.04.1
using the Bitnami Django Stack: bitnami-djangostack-2.1.5-1
I get this error when starting apache service:
[16:21:20.028270 2019] [wsgi:error] Traceback (most recent call last):
[16:21:20.028373 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/config.py", line 562, in configure
[16:21:20.028395 2019] [wsgi:error] handler = self.configure_handler(handlers[name])
[16:21:20.028421 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/config.py", line 735, in configure_handler
[16:21:20.028438 2019] [wsgi:error] result = factory(**kwargs)
[16:21:20.028463 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/handlers.py", line 147, in __init__
[16:21:20.028479 2019] [wsgi:error] BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
[16:21:20.028504 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/handlers.py", line 54, in __init__
[16:21:20.028520 2019] [wsgi:error] logging.FileHandler.__init__(self, filename, mode, encoding, delay)
[16:21:20.028545 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/__init__.py", line 1092, in __init__
[16:21:20.028562 2019] [wsgi:error] StreamHandler.__init__(self, self._open())
[16:21:20.028601 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/__init__.py", line 1121, in _open
[16:21:20.028618 2019] [wsgi:error] return open(self.baseFilename, self.mode, encoding=self.encoding)
[16:21:20.028666 2019] [wsgi:error] PermissionError: [Errno 13] Permission denied: '/opt/djangostack-2.1.5-1/apps/django/django_projects/myproject/static/logs/ca.myproject.app.cleaner.log'
[16:21:20.028701 2019] [wsgi:error]
[16:21:20.028716 2019] [wsgi:error] The above exception was the direct cause of the following exception:
[16:21:20.028729 2019] [wsgi:error]
[16:21:20.028753 2019] [wsgi:error] Traceback (most recent call last):
[16:21:20.028832 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/apps/django/django_projects/myproject/myproject/wsgi.py", line 19, in <module>
[16:21:20.028851 2019] [wsgi:error] application = get_wsgi_application()
[16:21:20.028886 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/apps/django/lib/python3.7/site-packages/Django-2.1.5-py3.7.egg/django/core/wsgi.py", line 12, in get_wsgi_application
[16:21:20.028903 2019] [wsgi:error] django.setup(set_prefix=False)
[16:21:20.028928 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/apps/django/lib/python3.7/site-packages/Django-2.1.5-py3.7.egg/django/__init__.py", line 19, in setup
[16:21:20.028944 2019] [wsgi:error] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[16:21:20.028968 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/apps/django/lib/python3.7/site-packages/Django-2.1.5-py3.7.egg/django/utils/log.py", line 76, in configure_logging
[16:21:20.028984 2019] [wsgi:error] logging_config_func(logging_settings)
[16:21:20.029008 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/config.py", line 799, in dictConfig
[16:21:20.029023 2019] [wsgi:error] dictConfigClass(config).configure()
[16:21:20.029046 2019] [wsgi:error] File "/opt/djangostack-2.1.5-1/python/lib/python3.7/logging/config.py", line 570, in configure
[16:21:20.029062 2019] [wsgi:error] '%r' % name) from e
[16:21:20.029102 2019] [wsgi:error] ValueError: Unable to configure handler 'cleaner_file'
How do I fix it?
Django is unable to create the log file in the directory /opt/djangostack-2.1.5-1/apps/django/django_projects/myproject/static/logs/
In my project settings.py the file path is specified as an absolute path, so Django should be able to write a log file to the folder.
Short fix:
chmod -R 777 $(pwd)/static/logs that will fix the issue, but after a reboot the error is back :(
Thanks for any help
Once the log hit the max size new file will be created with root permissions, see below:
-rw-rw-r-- 1 daemon daemon 0 Jun 5 13:54 my_project.debug.log
-rw-rw-r-- 1 daemon daemon 15319 Jun 12 17:52 my_project.app.log
**-rw-r--r-- 1 root root 159042 Jun 12 17:52 my_project.ws.log** --> HERE
-rw-rw-r-- 1 daemon daemon 15700891 Jun 12 17:52 my_project.ws.log.1
-rw-rw-r-- 1 daemon daemon 15711651 Jun 12 16:53 my_project.ws.log.2

Turbodbc returns ODBC error state 60 when run from apache2/mod_wsgi

I have a Django website that uses TURBODBC to execute some direct SQL calls on a legacy database. This works perfectly when using the built in django test server, but when I run it with Apache2 I get the error message: “ODBC error state: 60”. I feel like it has something to do with running it with mod_wsgi but I am not sure. I have had NO luck with Google. Not even sure what “state: 60” means.
Internal Server Error: /inventory/in/
[Mon Apr 09 22:01:09.718077 2018] [wsgi:error] [pid 10074:tid 140709839501056] Traceback (most recent call last):
[Mon Apr 09 22:01:09.718147 2018] [wsgi:error] [pid 10074:tid 140709839501056] File "/usr/local/lib/python3.5/dist-packages/turbodbc/exceptions.py", line 50, in wrapper
[Mon Apr 09 22:01:09.718213 2018] [wsgi:error] [pid 10074:tid 140709839501056] return f(*args, **kwds)
[Mon Apr 09 22:01:09.718282 2018] [wsgi:error] [pid 10074:tid 140709839501056] File "/usr/local/lib/python3.5/dist-packages/turbodbc/connect.py", line 44, in connect
[Mon Apr 09 22:01:09.718347 2018] [wsgi:error] [pid 10074:tid 140709839501056] turbodbc_options))
[Mon Apr 09 22:01:09.718410 2018] [wsgi:error] [pid 10074:tid 140709839501056] turbodbc_intern.Error: ODBC error
[Mon Apr 09 22:01:09.718493 2018] [wsgi:error] [pid 10074:tid 140709839501056] ] state: 60
Questions:
What does "state 60" mean?
What is causing this error?
How do I correct the issue?
simple program to reproduce the error.
from turbodbc import connect
def application(environ, start_response):
status = '200 OK'
output = b'Hello World!'
connect_to_db()
response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
def connect_to_db():
connection = connect(dsn="odbc-dev", uid='foo', pwd='bar')
cursor = connection.cursor()
apache2 config:
WSGIScriptAlias / /srcPython3/wsgi_test/hello_world.py
WSGIPythonPath /srcPython3/wsgi_test/
<VirtualHost *:80>
<Directory /srcPython3/wsgi_test>
<Files hello_world.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
error.log
mod_wsgi (pid=11944): Exception occurred processing WSGI script '/srcPython3/wsgi_test/hello_world.py'.
[wsgi:error] Traceback (most recent call last):
[wsgi:error] File "/usr/local/lib/python3.5/dist-packages/turbodbc/exceptions.py", line 50, in wrapper
[wsgi:error] return f(*args, **kwds)
[wsgi:error] File "/usr/local/lib/python3.5/dist-packages/turbodbc/connect.py", line 44, in connect
[wsgi:error] turbodbc_options))
[wsgi:error] turbodbc_intern.Error: ODBC error
[wsgi:error] state: 60
[wsgi:error]
[wsgi:error] During handling of the above exception, another exception occurred:
[wsgi:error]
[wsgi:error] Traceback (most recent call last):
[wsgi:error] File "/srcPython3/wsgi_test/hello_world.py", line 7, in application
[wsgi:error] connect_to_db()
[wsgi:error] File "/srcPython3/wsgi_test/hello_world.py", line 15, in connect_to_db
[wsgi:error] connection = connect(dsn="odbc-dev", uid='foo', pwd='bar')
[wsgi:error] File "/usr/local/lib/python3.5/dist-packages/turbodbc/exceptions.py", line 52, in wrapper
[wsgi:error] raise DatabaseError(str(e))
[wsgi:error] turbodbc.exceptions.DatabaseError: ODBC error
[wsgi:error] state: 60
thanks
One of the issues that can arise with database access under mod_wsgi is that your code runs as the Apache user and not you. Also, environment variables are not inherited from your user login profile.
So if the access mechanism results in using the username of the UNIX account code is running as it will not work. And will not work if you are setting environment variables in your personal account and your code was expecting to see those.
For the first problem, ensure you are using mod_wsgi daemon mode, and then configure daemon mode to run your code as you, and not the Apache user.
For the second, set the environment variables in your WSGI script file before importing any other code.

AWS Elastic Beanstalk Flask Application: Toolkit Can't Find Resource That Appears to be Present on EC2 Instance

I'm trying to deploy a flask/python app using AWS Elastic Beanstalk and getting a '500 internal server' error resulting from a missing resource. The app works locally but one of the backend components can't find a resource it needs when running on the EC2 instance that Elastic Beanstalk is managing.
I am using the Natural Language Toolkit which I include in my requirements.txt file to be downloaded as a pip package. The nltk package install seems to be have been successful as I'm not getting an error on the line:
import nltk
The line I am getting the error on in my application code is:
sent_detector = nltk.data.load('tokenizers/punkt/english.pickle')
The error I am getting in my log ending is:
[Wed Feb 14 22:17:10.731016 2018] [:error] [pid 13894] [remote 172.31.0.22:252] Resource \x1b[93mpunkt\x1b[0m not found.
[Wed Feb 14 22:17:10.731018 2018] [:error] [pid 13894] [remote 172.31.0.22:252] Please use the NLTK Downloader to obtain the resource:
[Wed Feb 14 22:17:10.731020 2018] [:error] [pid 13894] [remote 172.31.0.22:252]
[Wed Feb 14 22:17:10.731023 2018] [:error] [pid 13894] [remote 172.31.0.22:252] \x1b[31m>>> import nltk
[Wed Feb 14 22:17:10.731025 2018] [:error] [pid 13894] [remote 172.31.0.22:252] >>> nltk.download('punkt')
[Wed Feb 14 22:17:10.731027 2018] [:error] [pid 13894] [remote 172.31.0.22:252] \x1b[0m
[Wed Feb 14 22:17:10.731029 2018] [:error] [pid 13894] [remote 172.31.0.22:252] Searched in:
[Wed Feb 14 22:17:10.731031 2018] [:error] [pid 13894] [remote 172.31.0.22:252] - '/home/wsgi/nltk_data'
[Wed Feb 14 22:17:10.731034 2018] [:error] [pid 13894] [remote 172.31.0.22:252] - '/usr/share/nltk_data'
[Wed Feb 14 22:17:10.731036 2018] [:error] [pid 13894] [remote 172.31.0.22:252] - '/usr/local/share/nltk_data'
[Wed Feb 14 22:17:10.731038 2018] [:error] [pid 13894] [remote 172.31.0.22:252] - '/usr/lib/nltk_data'
[Wed Feb 14 22:17:10.731040 2018] [:error] [pid 13894] [remote 172.31.0.22:252] - '/usr/local/lib/nltk_data'
[Wed Feb 14 22:17:10.731043 2018] [:error] [pid 13894] [remote 172.31.0.22:252] - '/opt/python/run/venv/nltk_data'
[Wed Feb 14 22:17:10.731045 2018] [:error] [pid 13894] [remote 172.31.0.22:252] - '/opt/python/run/venv/lib/nltk_data'
[Wed Feb 14 22:17:10.731047 2018] [:error] [pid 13894] [remote 172.31.0.22:252] - ''
[Wed Feb 14 22:17:10.731049 2018] [:error] [pid 13894] [remote 172.31.0.22:252]
When I added the line
nltk.download('punkt')
to my application in order to ensure that the resource I need would be downloaded, I get this message in the error log:
[Wed Feb 14 22:30:07.861273 2018] [:error] [pid 28765] [nltk_data] Downloading package punkt to /home/wsgi/nltk_data...
which is then followed by a series of errors that comes down to:
[Wed Feb 14 22:30:07.864521 2018] [:error] [pid 28765] [remote 172.31.0.22:55448] FileNotFoundError: [Errno 2] No such file or directory: '/home/wsgi/nltk_data'
So I SSH-d into my EC2 instance, entered the virtual environment that it seems like my app is running on from the opt/python/run directory using
$source venv/bin/activate
and opened up the python interpreter. When I ran
>>import nltk
>>nltk.download('punkt')
I got back
[nltk_data] Downloading package punkt to /home/ec2-user/nltk_data...
[nltk_data] Package punkt is already up-to-date!
True
So I also tried
>>> nltk.data.load('tokenizers/punkt/english.pickle')
and got back:
<nltk.tokenize.punkt.PunktSentenceTokenizer object at 0x7fb8afd34080>
So, it seems like the nltk package on my EC2 instance knows where the nltk_data resource is as long as it's not being asked by my Flask application. I also tried entering
>>nltk.data.path.append('home/ec2-user/nltk_data')
and still got the same error as I posted above with no indication that my attempts append the list of paths to check for nltk_data had gone through.
I am not sure what I need to get nltk to locate where the nltk_data resource it is trying to find is located.
I have seen .ebextensions mentioned in reference to dependency issues and tried to read the AWS page about it, but am not sure exactly how it fits into the issue occurring with my application. Probably a learning-curve web dev literacy issue on my end.
Thanks for any clarity that can be provided regarding this situation!
I had the exact same problem when deploying a FastAPI endpoint to Elastic Beanstalk. I suggest you setup your EB platform to run Docker, and then download the nltk models at build time. Your Dockerfile will look like the following:
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
RUN pip install --upgrade pip
# Install requirements
COPY ./app/requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
RUN python -m nltk.downloader 'popular'
# Tell the base image where to find the application entrypoint
ENV MODULE_NAME=app.main
ENV VARIABLE_NAME=api
# Copy required packages and source files
COPY . .
EXPOSE 80
I resolved this by SSH-ing into my instance and downloading the nltk_data resource using the following command from http://www.nltk.org/data.html :
sudo python -m nltk.downloader -d /usr/local/share/nltk_data all
after which the nltk module didn't have any problem finding the resource.

Django stops working with RuntimeError: populate() isn't reentrant

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.
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.
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:
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.
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.
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
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.
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.
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
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.
This error also generated if inconsistence use of space and tab in the code.
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.
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 :)
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
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 !!
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'
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.
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.
In my case, I had a circular import, which cause an error that break the populate method.
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.
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
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...
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.
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.
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.
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.
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.
Removing the virtualenv directory, recreating the virtualenv, then reinstalling all the requirements fixed it for me.
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.
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.
Restarting the Apache server for me solved the problem. You can do that by using the command
$ sudo service apache2 restart
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
In apps.py, i have set different name from my app name.