0

I was trying to connect to the database by using the command

from django.db import connection 

from the python shell but this produced the following Import error

File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 11, in <module>
    if DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 184, in inner
    self._setup()
  File "/usr/local/lib/python2.7/dist-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.

Im using Django 1.4 and PostgreSQL the same command was executed sucessfully when i was using the earlier versions of django Plz help.

nimeshkiranverma
  • 1,408
  • 6
  • 25
  • 48

3 Answers3

1

You need to set an OS environ value for DJANGO_SETTINGS_MODULE. You can do that with a simple EXPORT command. If you are using virtualenv, I hightly recommend virtualenvwrapper and I put this setting in the 'bin/postactivate' file.

Note: I just tested it via the django 1.4 shell and the import works fine for me.

David S
  • 12,967
  • 12
  • 55
  • 93
0

Your manage.py file defines DJANGO_SETTINGS_MODULE. Since your running this code outside of your django application, you need to define it yourself.

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", <location of settings.py>)
btstevens89
  • 147
  • 2
  • 10
  • My manage.py file already conatins the right location on my settings.py – nimeshkiranverma Sep 22 '12 at 17:48
  • Yeah, that's what I was saying. When you are in a basic python shell, you never run the line in manage.py that sets DJANGO_SETTINGS_MODULE. Therefore it's not available to you in that instance. – btstevens89 Sep 22 '12 at 20:38
0

You have to set the environment variable DJANGO_SETTINGS_MODULE. The easiest way to do it is to put the following code at the top of the file that contains your failing import statement:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'path/to/your/settings.py'
# ...
from django.db import connection
pemistahl
  • 9,304
  • 8
  • 45
  • 75
  • I m trying to execute "from django.db import connection" through python shell on the terminal, plz guide me what i have to do in this case. – nimeshkiranverma Sep 22 '12 at 17:00
  • Are you using the Django shell (`python manage.py shell`) from within your Django project? I think you don't. If you use the Django shell, all the necessary environment variables are set automatically, so that you don't have to care about it. – pemistahl Sep 22 '12 at 17:18
  • Actually im using the Django shell from my django Project. the command ran without problem in previous django verision,it creating prob in django 1.4 – nimeshkiranverma Sep 22 '12 at 17:43
  • That's strange, for me it works just fine with Django 1.4. But I'm using SQlite. Since your failing import statement seems to call your database, it might be an issue with PostgreSQL. If this is the case, I cannot help you (no experience with PostgreSQL). – pemistahl Sep 22 '12 at 18:15