How to Solve Error ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Posted on

This is an article where the article itself is actually focusing on how to solve error. The error itself is actually specifying about an error triggered upon executing a web-based application powered by Django framework. Below is the log file which is actually generated :

 mod_wsgi (pid=17877): Target WSGI script '/home/user/apps/user/src/wsgi.py' cannot be loaded as Python module.
[Wed Feb 28 21:17:06.710122 2018] [wsgi:error] [pid 17877] [remote 127.0.0.1:1959] mod_wsgi (pid=17877): Exception occurred processing WSGI script '/home/user/apps/user/src/wsgi.py'.
[Wed Feb 28 21:17:06.710152 2018] [wsgi:error] [pid 17877] [remote 127.0.0.1:1959] Traceback (most recent call last):
[Wed Feb 28 21:17:06.710172 2018] [wsgi:error] [pid 17877] [remote 127.0.0.1:1959]   File "/home/user/apps/user/src/wsgi.py", line 9, in                                                                                
[Wed Feb 28 21:17:06.710242 2018] [wsgi:error] [pid 17877] [remote 127.0.0.1:1959]     application = get_wsgi_application()                                                                                                       
[Wed Feb 28 21:17:06.710258 2018] [wsgi:error] [pid 17877] [remote 127.0.0.1:1959]   File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application                                          
[Wed Feb 28 21:17:06.710457 2018] [wsgi:error] [pid 17877] [remote 127.0.0.1:1959]     django.setup()                                                                                                                             
[Wed Feb 28 21:17:06.710473 2018] [wsgi:error] [pid 17877] [remote 127.0.0.1:1959]   File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 17, in setup                                                          
[Wed Feb 28 21:17:06.710665 2018] [wsgi:error] [pid 17877] [remote 127.0.0.1:1959]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)                                                                               
[Wed Feb 28 21:17:06.710678 2018] [wsgi:error] [pid 17877] [remote 127.0.0.1:1959]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 55, in __getattr__                                               
[Wed Feb 28 21:17:06.710917 2018] [wsgi:error] [pid 17877] [remote 127.0.0.1:1959]     self._setup(name)                                                                                                                          
[Wed Feb 28 21:17:06.710933 2018] [wsgi:error] [pid 17877] [remote 127.0.0.1:1959]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 41, in _setup                                                    
[Wed Feb 28 21:17:06.710951 2018] [wsgi:error] [pid 17877] [remote 127.0.0.1:1959]     % (desc, ENVIRONMENT_VARIABLE))                                                                                                            
[Wed Feb 28 21:17:06.710976 2018] [wsgi:error] [pid 17877] [remote 127.0.0.1:1959] ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.       

The specific error is actually presented by the last line of the error which is shown as follows :

ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

The error above is still related to the failure of executing wsgi.py file as application callable for deploying or running a Python web-based application. To solve the above error, just add the following line :

os.environ.setdefault("DJANGO_SETTINGS_MODULE","app.settings")

It is actually the sole solution since the error is actually pointing out the error specifying also the solution for it. It must be done by either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. So, the solutiono done is by defining the environment variable as shown above. The value of the environment variable must be initialized with the file named settings located in the application.

Leave a Reply