How to Solve Error Message SyntaxError: EOL while scanning string literal in Python Django-web based Application

Posted on

This is just an article to show how to solve a specific error message. That error message is clear there is a string literal without the end of it in the line. The scenario is to try to save or to entry a data through a form. After successfully preparing the form, the entry process is at last possible. When performing the entry process or storing the data through the form, the error appears. This is the error message showing the problem :

>>> form = UserForm({name:"Mike"})'
  File "", line 1
    form = CategoryForm({name:"Mike"})'
                                   ^
SyntaxError: EOL while scanning string literal

Basically, the error above occurs after series of execution process. The following are that series of processes :

1. Executing the Python Shell Console using the following command :

user@hostnam:~e$ python manage.py shell
Python 2.7.15+ (default, Nov 27 2018, 23:36:35) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

2. Importing os module so the environment variable for defining the Django settings file is possible. The following is the detail of the process :

>>> import os;
>>> os.environ['DJANGO_INSTALLED_MODULE'] = 'app.settings';

3. After that, the process continue on importing django module. It is important because importing the django module is a necessary for importing the form in the next step for the entry step. Below is the script execution :

>>> import django;
>>> django.setup();

4. Finally, the process is finally on the entry to the form. Below is the script execution :

>>>> form = UserForm({name:"Mike"})'
  File "", line 1
    form = CategoryForm({name:"Mike"})'
                                   ^
SyntaxError: EOL while scanning string literal

Apparently, failing the saving process above, it generates error message ‘SyntaxError: EOL while scanning string literal’. Basically, it is a simple error that can happen in any kinds of execution of the script. It is because there is a (‘) single quote character in the end of the script. That single (‘) quote will act as an opening single quote without any closing single quote. Since there is no closing single quote, it will generate that kind of error. The solution is quite simple to solve this problem. Just remove the (‘) single quote. Replace it with another character to terminate or to signal the end of the script. That character is the (;) semicolon character.

So, the solution is to remove the single quote and replace is with a semicolon to signal or to inform that the line of script has end.

Leave a Reply