David J.
David J.

Reputation: 1913

environment variable for django settings?

I've been using Django for over a year and have never needed to 'define the environment variable DJANGO_SETTINGS_MODULE as I am asked to do in the following error message:

E
======================================================================
ERROR: setUpClass (__main__.SmokeTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 1026, in setUpClass
    if not connections_support_transactions():
  File "C:\Python27\lib\site-packages\django\test\testcases.py", line 991, in connections_support_transactions
    for conn in connections.all())
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 227, in all
    return [self[alias] for alias in self]
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 224, in __iter__
    return iter(self.databases)
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 157, in databases
    self._databases = settings.DATABASES
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 55, in __getattr__
    self._setup(name)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 41, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

I am using Windows command line.

I tried running

from django.conf import settings

settings.configure()

as explained here, but this didn't help.

Does anyone know how to get this to work?

Upvotes: 0

Views: 2150

Answers (2)

bruno desthuilliers
bruno desthuilliers

Reputation: 77942

"@brunodesthuilliers in the command line I am in my app directory, I run python tests.py "

Well that's not going to work (at least not that easily). Why don't you just use the builtin management "test" command ?

 #> cd your/project/root 
 #> python manage.py test

cf https://docs.djangoproject.com/en/1.9/topics/testing/

Upvotes: 1

Wiriya Rungruang
Wiriya Rungruang

Reputation: 318

Are you sure in your manage.py have something like this

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "projectname.settings")

https://docs.djangoproject.com/en/1.9/ref/django-admin/

Upvotes: 1

Related Questions