Discussion:
How to run Python script? (configure settings error)
(too old to reply)
Daniel Grace
2014-08-13 14:21:07 UTC
Permalink
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', '***@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.
--
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.
Tom Evans
2014-08-13 14:34:25 UTC
Permalink
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
Post by Daniel Grace
from django.db import transaction
from django.contrib.auth.models import User
from flow.models import States, Types, Docs, Logs
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()
'johnpass')
user1.save()
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'
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>
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.
--
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
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.
--
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/CAFHbX1K_oF52T1DWgzzx3ZKOyPsg1-jCaWRdRyT4wesaOgH78A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Andrew Farrell
2014-08-13 14:36:37 UTC
Permalink
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
Post by Daniel Grace
from django.db import transaction
from django.contrib.auth.models import User
from flow.models import States, Types, Docs, Logs
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()
'johnpass')
user1.save()
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'
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>
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.
--
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
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
<https://groups.google.com/d/msgid/django-users/9b3a4974-26fd-4229-a3bf-7d7ce28dbdfe%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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/CA%2By5TLagUx4xOUuQfANJ8sAcm5miaVYyv2cUzY4h8uQzGSPzwA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Andrew Farrell
2014-08-13 14:39:56 UTC
Permalink
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`
Post by Andrew Farrell
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.
from django.core.management.base import BaseCommand
args = 'spam eggs'
help = 'makes an omelette to throw at a silly English knigget'
print settings.STATIC_ROOT
Post by Daniel Grace
from django.db import transaction
from django.contrib.auth.models import User
from flow.models import States, Types, Docs, Logs
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()
'johnpass')
user1.save()
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'
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>
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.
--
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
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
<https://groups.google.com/d/msgid/django-users/9b3a4974-26fd-4229-a3bf-7d7ce28dbdfe%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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/CA%2By5TLZKx2wGgOwoyGxqzZ-hMxwpM-YiF8sN%3Dm7P0cBEH9GmQA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Daniel Grace
2014-08-13 14:53:43 UTC
Permalink
I used this method and got my script working OK.
Post by Andrew Farrell
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.
from django.core.management.base import BaseCommand
args = 'spam eggs'
help = 'makes an omelette to throw at a silly English knigget'
print settings.STATIC_ROOT
--
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/77fa1182-7ee1-48ef-bbe3-7b7235087307%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Collin Anderson
2014-08-13 14:38:38 UTC
Permalink
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.
--
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/c86b5aca-e7a0-44e5-95f8-505d8e44c40a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...