DJANGO_SETTINGS_MODULE - undefined with gis.db

when i use

from django.contrib.gis.db import models 

I get an error:

 Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> from django.contrib.gis.db import models File "C:\Python27\lib\site-packages\django\contrib\gis\db\models\__init__.py" , line 2, in <module> from django.db.models import * File "C:\Python27\lib\site-packages\django\db\__init__.py", line 11, in <module> if DEFAULT_DB_ALIAS not in settings.DATABASES: File "C:\Python27\lib\site-packages\django\utils\functional.py", line 184, in inner self._setup() File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 40, in _setup raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE) ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined. 

how to fix it ??

Using

 from django.db import models 

django work no problem

+6
source share
2 answers

There are two ways to fix this:

  • you need to add the settings module and set DJANGO_SETTINGS_MODULE to point to it, or
  • use settings.configure to bypass the variable DJANGO_SETTINGS_MODULE env.

The second option is good for using django parts without actually setting up everything that is needed for the project.

+4
source

gis.db not essential in your question.

The difference between the two import commands is only in the context of how you run them, because django.contrib.gis.db.models does not do anything more than import some empty __init__.py files before importing django.db.models .

The easiest way to test something in the right environment is with control commands, for example.

 $ python manage.py shell # now you are sure that django.conf settings have been imported >>> from django.contrib.gis.db import models >>> from django.db import models # Both will equally succeed (or maybe fail for another reason) 

(similar question Django Documentation: Models; error from line 1 of the code )

+2
source

All Articles