How to Resolve Error Message export DJANGO_SETTINGS_MODULE=”app.settings” SyntaxError: invalid syntax

Posted on

Introduction

This article is about how to resolve the error message as shows in the title. The error appear when the execution of a command which is also exist in the title of the article. That command is the command ‘export DJANGO_SETTINGS_MODULE=”app.settings”. The command has a special purpose to export an environment variable. The environment variable is ‘DJANGO_SETTINGS_MODULE and the value of it is ‘app.settings’. The value refers to any name of the application installed in Django-based framework project. At first, the main purpose is to test to entry data through a class via python command shell. What is the main reason for executing the above the command ?, the following is one of the trigger for defining that environment variable :

(env) user@hostname:~/python/django/project$ python
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from app.models import MyClass;
Traceback (most recent call last):
  File "", line 1, in 
  File "/home/user/python/django/project/app/models.py", line 6, in 
    class MyClass(models.Model):
  File "/home/user/python/django/project/env/lib/python3.6/site-packages/django/db/models/base.py", line 103, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/user/python/django/project/env/lib/python3.6/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
    self.check_apps_ready()
  File "/home/user/python/django/project/env/lib/python3.6/site-packages/django/apps/registry.py", line 134, in check_apps_ready
    settings.INSTALLED_APPS
  File "/home/user/python/django/project/env/lib/python3.6/site-packages/django/conf/__init__.py", line 79, in __getattr__
    self._setup(name)
  File "/home/user/python/django/project/env/lib/python3.6/site-packages/django/conf/__init__.py", line 64, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
>>>

As in the above output message shows, importing a class with the name of MyClass end in a failure. In order be able to import it, there is a requirement for defining DJANGO_SETTINGS_MODULE. Apparently, in attempt to define ‘DJANGO_SETTINGS_MODULE’ which is an environment variable, the process stop because of the appearance of an error. The following is the complete output of the above command execution with the generated error :

(env) user@hostname:~/python/django/project$ python
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> export DJANGO_SETTINGS_MODULE=project.settings
  File "", line 1
    export DJANGO_SETTINGS_MODULE=project.settings
                                ^
SyntaxError: invalid syntax
>>>

The error above appear with the following steps of execution :

1. Activate the python virtual environment. Execute the following command to activate it :

source env_folder/bin/activate

The following is the real command execution of the above command pattern for activating the python virtual environment :

user@hostname:~/python/django/project$ source env/bin/activate
(env) user@hostname:~/python/django/project$

2. Enter the python command shell or python console. Just execute the following command for entering the python command shell :

(env) user@hostname:~/python/django/project$ python
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

3. Finally, execute the command ‘export DJANGO_SETTINGS_MODULE=app.settings’ in the python command shell or python console. Execute it as follows :

>>> export DJANGO_SETTINGS_MODULE=project.settings
  File "", line 1
    export DJANGO_SETTINGS_MODULE=project.settings
                                ^
SyntaxError: invalid syntax
>>> 

Solution

The solution to solve the above problem is simple. The following are the solution in order to be able to import the class :

1. Import a package with the name of ‘os’. Execute the following command in the python shell or python command console :

>>> import os;

2. Execute the following command to define the environment variable.

>>> os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings';

Fortunately, the above steps is a success. So, in order to define the environment variable, the above steps are the success method in this context.

Leave a Reply