user7172
user7172

Reputation: 974

DJANGO_SETTINGS_MODULE is undefined with gis.db

when I use

from django.contrib.gis.db import models

I get 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??

When use

from django.db import models

the django work without problems

Upvotes: 6

Views: 253

Answers (2)

hynekcer
hynekcer

Reputation: 15548

gis.db is not essential in your question.

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

The easiest way how to test anything in the correct environment is by management commands, e.g.

$ 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)

(A similar question Django documentation: Models; error from line 1 of code)

Upvotes: 2

dnozay
dnozay

Reputation: 24314

There are two ways to fix this:

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

The second alternative is good to use parts of django without actually setting up everything that's needed for a project.

Upvotes: 4

Related Questions