How to run Python script? (configure settings error)

264 views
Skip to first unread message

Daniel Grace

unread,
Aug 13, 2014, 2:21:07 PM8/13/14
to django...@googlegroups.com
I have a Python script as follows:

from django.db import transaction
from django.contrib.auth.models import User
from flow.models import States, Types, Docs, Logs

if __name__ == '__main__':
    with transaction.atomic():
        models.Logs.objects.all().delete()
        models.Docs.objects.all().delete()
        models.States.objects.all().delete()
        models.Types.objects.all().delete()
        models.User.objects.all().delete()
        user1 = User.objects.create_user('john', 'xxx...@gmail.com', 'johnpass')
        user1.save()

When I run this in IDLE I get the following error:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 38, in _setup
    settings_module = os.environ[ENVIRONMENT_VARIABLE]
  File "C:\Python34\lib\os.py", line 651, in __getitem__
    raise KeyError(key) from None
KeyError: 'DJANGO_SETTINGS_MODULE'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Daniel\Documents\Python\cresta\flow\test_data.py", line 7, in <module>
    from django.contrib.auth.models import User
  File "C:\Python34\lib\site-packages\django\contrib\auth\__init__.py", line 6, in <module>
    from django.middleware.csrf import rotate_token
  File "C:\Python34\lib\site-packages\django\middleware\csrf.py", line 14, in <module>
    from django.utils.cache import patch_vary_headers
  File "C:\Python34\lib\site-packages\django\utils\cache.py", line 26, in <module>
    from django.core.cache import get_cache
  File "C:\Python34\lib\site-packages\django\core\cache\__init__.py", line 69, in <module>
    if DEFAULT_CACHE_ALIAS not in settings.CACHES:
  File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 54, in __getattr__
    self._setup(name)
  File "C:\Python34\lib\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.

How do I configure these settings?

Thanks.

Tom Evans

unread,
Aug 13, 2014, 2:34:50 PM8/13/14
to django...@googlegroups.com
You cannot use django before configuring it.

https://docs.djangoproject.com/en/1.6/topics/settings/#designating-the-settings

Django provides several entry points, manage.py and wsgi.py, which
perform this configuration steo.

If you choose not to use those entry points, you are required to
configure it yourself manually:

https://docs.djangoproject.com/en/1.6/topics/settings/#either-configure-or-django-settings-module-is-required

Cheers

Tom
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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/9b3a4974-26fd-4229-a3bf-7d7ce28dbdfe%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Andrew Farrell

unread,
Aug 13, 2014, 2:37:30 PM8/13/14
to django...@googlegroups.com
Note: this is much better documented at https://docs.djangoproject.com/en/dev/howto/custom-management-commands/

You want to move this file to the management.commands module. I don't know what the equivalent commands are on windows, but on linux this would be.
1) `cd myapp; mkdir management`: Within you app, in the same directory as views.py create a directory called 'management'
2) `cd management`: Go into that directory
3) `touch__init__.py`: create an empty file named __init__.py
4) `mkdir commands`: create a directory named "commands"
5) `cd commands`: go into that directory
6) `touch __init__.py`: create another empty file named__init.py
7) `echo 'from django.conf import settings' > mycommandname.py`: create a file named with the command you want within that directory.
8) You need to have something in that file that looks like:

from django.core.management.base import BaseCommand
class Command(BaseCommand):
    args = 'spam eggs'
    help = 'makes an omelette to throw at a silly English knigget'
    def handle(self, *args, **swargs):
        print settings.STATIC_ROOT


--

Collin Anderson

unread,
Aug 13, 2014, 2:38:39 PM8/13/14
to django...@googlegroups.com
set: os.environ['DJANGO_SETTINGS_MODULE'] = 'yourproject.settings' (or see how it's done in manage.py)

On 1.7 call django.setup() first before using models.
On 1.6 or lower, you may need to call models.get_models() before using your models.

Andrew Farrell

unread,
Aug 13, 2014, 2:41:13 PM8/13/14
to django...@googlegroups.com
oh, and you should then be able to run the command by doing
python manage.py myapp mycommand
and see the 'mycommand' in the list of commands printed by `python manage.py help`

Daniel Grace

unread,
Aug 13, 2014, 2:53:43 PM8/13/14
to django...@googlegroups.com
I used this method and got my script working OK.
Reply all
Reply to author
Forward
0 new messages