Wednesday, October 1, 2014

Python question about positioning of "from... import" statement

Hi, this is more of a Python question but here is something I don't understand in a Django script.
The following script works fine:

import os
import django

if __name__ == '__main__':
    print("Starting Rango delete data script...")
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tango.settings')
    django.setup()
    from rango.models import Category, Page
    Page.objects.all().delete()
    Category.objects.all().delete()
    print("Deleted all data from tables: Page and Category.")

...but when I move the "from... import" statement to the top of the file:

import os
import django
from rango.models import Category, Page

if __name__ == '__main__':
    print("Starting Rango delete data script...")
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tango.settings')
    django.setup()
    Page.objects.all().delete()
    Category.objects.all().delete()
    print("Deleted all data from tables: Page and Category.")

...I get an error:

  File "C:\landy\lib\site-packages\django\conf\__init__.py", line 40, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TAB
LESPACE, but settings are not configured. You must either define the environment
 variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing s
ettings.

What is going on?

Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/955fb638-1724-4115-951a-668218e68771%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment