How to Solve Error NameError: name ‘keyword’ is not defined on executing Django Application

Posted on

The error is specified in the title of this article which is : “name ‘keyword’ is not defined”. It is actually a simple error which can be solved. But before deciding on how to solve it, the error itself happened upon executing a web-based powered by Django framework. It is happened when wsgi.py file is being executed. Definitely the error concerns about a reserved keyword named ‘keyword’ which is considered as not defined so it is defined as a NameError.

In the context of this article, the error is actually defined as follows :

[Wed Feb 28 22:12:12.023560 2018] [wsgi:error] [pid 17878] [remote 127.0.0.1:1959] mod_wsgi (pid=17878): Target WSGI script '/home/user/apps/user/src/wsgi.py' cannot be loaded as Python module.                               
[Wed Feb 28 22:12:12.023594 2018] [wsgi:error] [pid 17878] [remote 127.0.0.1:1959] mod_wsgi (pid=17878): Exception occurred processing WSGI script '/home/user/apps/user/src/wsgi.py'.                                          
[Wed Feb 28 22:12:12.023614 2018] [wsgi:error] [pid 17878] [remote 127.0.0.1:1959] Traceback (most recent call last):                                                                                                             
[Wed Feb 28 22:12:12.023630 2018] [wsgi:error] [pid 17878] [remote 127.0.0.1:1959]   File "/home/user/apps/user/src/wsgi.py", line 7, in                                                                                
[Wed Feb 28 22:12:12.023680 2018] [wsgi:error] [pid 17878] [remote 127.0.0.1:1959]     os.environ.setdefault("DJANGO_SETTINGS_MODULE","user.settings")
[Wed Feb 28 22:12:12.023697 2018] [wsgi:error] [pid 17878] [remote 127.0.0.1:1959] NameError: name 'os' is not defined

The error can actually be seen throught the information given in the error log given above. It is located in a file named ‘wsgi.py’ in line 7 which is shown as follows :

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

The error defined above is actually part of the settings for deploying web-based application which is powered by Django framework written in Python. But the actual part which is needed to be figured out is the reserved keyword ‘os’. It cannot be recognized although it is actually a part of the environment variable named ‘DJANGO_SETTINGS_MODULE’. But the reserved keyword, ‘os’ which is part of the definition of the environment variable ‘DJANGO_SETTINGS_MODULE’ is not recognized. It is considered as not defined. So, to solve it, one way that can be used to solve it is by importing it. If the reserved keyword is actually a library which can be be imported, just import it as follows :

import os

After defining it in the file named ‘wsgi.py’, re-execute the web-based application.

Leave a Reply