[Answer]-From django.db import connection giving error in django1.4

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.

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

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

Leave a comment