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
There are two ways to fix this:
settings
module in and set DJANGO_SETTINGS_MODULE
to point to it, orsettings.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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With