Estou fazendo alguns testes em um projeto Django. Criei um módulo bem simples só para testes mesmo. Segue abaixo:
from django.contrib.auth.models import User
def teste():
for i in User.objects.all():
print i.username
if __name__ == "__main__":
teste()
Ao executar o código, esse erro é retornado:
Traceback (most recent call last): File "teste.py", line 6, in from django.contrib.auth.models import User File "/home/carlos/myworkspace/helpdesk/lib/python2.7/site-packages/django /contrib/auth/init.py", line 6, in from django.middleware.csrf import rotate_token File "/home/carlos/myworkspace/helpdesk/lib/python2.7/site-packages/django /middleware/csrf.py", line 14, in from django.utils.cache import patch_vary_headers File "/home/carlos/myworkspace/helpdesk/lib/python2.7/site-packages/django/utils/cache.py", line 26, in from django.core.cache import get_cache File "/home/carlos/myworkspace/helpdesk/lib/python2.7/site-packages/django/core/cache/init.py", line 69, in if DEFAULT_CACHE_ALIAS not in settings.CACHES: File "/home/carlos/myworkspace/helpdesk/lib/python2.7/site-packages/django/conf/init.py", line 54, in getattr self._setup(name) File "/home/carlos/myworkspace/helpdesk/lib/python2.7/site-packages/django/conf/init.py", line 47, in _setup % (desc, ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
OBS.: Dentro do python manage.py shell
com o comando %run teste.py, o código funciona.