Running a stand-alone script executing a typical request in Django with parameters `settings / dev.py` instead of` settings.py`

Pay attention to settings/dev.py instead of a single settings.py and script.py in my_app in the following Django project (1.4.3):

 . ├── my_project │ ├── my_app │ │ ├── __init__.py │ │ ├── models.py │ │ ├── tests.py │ │ ├── views.py │ │ └── script.py │ ├── __init__.py │ ├── settings │ │ ├── dev.py │ │ ├── __init__.py │ │ └── prod.py │ ├── urls.py │ └── wsgi.py ├── manage.py └── requirements.txt 

When I had only one settings.py file instead of the settings folder, I was able to run the following script without errors :

script.py:

 ################################################################### # set up for making it possible to run a model query from my script. ################################################################### import os import sys sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))) from django.core.management import setup_environ import settings setup_environ(settings) #################################################################### from my_app.models import MyModel all_entries = MyModel.objects.all() 

Btw, I got this from the second approach in this article , my setup is several lines longer because my script.py is in my_app instead of directly in the my_project folder.

Since I now use settings/dev.py instead of settings.py , I changed the last two lines of my installation in my script to the following:

 import settings.dev import setup_environ(settings.dev) 

But when I ran my script now , I get this error:

 Traceback (most recent call last): File "my_script.py", line 12, in <module> all_entries = MyModel.objects.all() File "/home/my_username/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/db/models/manager.py", line 131, in get return self.get_query_set().get(*args, **kwargs) File "/home/my_username/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/db/models/query.py", line 358, in get clone = self.filter(*args, **kwargs) File "/home/my_username/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/db/models/query.py", line 624, in filter return self._filter_or_exclude(False, *args, **kwargs) File "/home/my_username/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/db/models/query.py", line 642, in _filter_or_exclude clone.query.add_q(Q(*args, **kwargs)) File "/home/my_username/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1250, in add_q can_reuse=used_aliases, force_having=force_having) File "/home/my_username/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1122, in add_filter process_extras=process_extras) File "/home/my_username/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1304, in setup_joins field, model, direct, m2m = opts.get_field_by_name(name) File "/home/my_username/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/db/models/options.py", line 311, in get_field_by_name cache = self.init_name_map() File "/home/my_username/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/db/models/options.py", line 341, in init_name_map for f, model in self.get_all_related_m2m_objects_with_model(): File "/home/my_username/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/db/models/options.py", line 429, in get_all_related_m2m_objects_with_model cache = self._fill_related_many_to_many_cache() File "/home/my_username/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/db/models/options.py", line 443, in _fill_related_many_to_many_cache for klass in get_models(only_installed=False): File "/home/my_username/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/db/models/loading.py", line 181, in get_models self._populate() File "/home/my_username/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/db/models/loading.py", line 64, in _populate self.load_app(app_name, True) File "/home/my_username/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/db/models/loading.py", line 86, in load_app app_module = import_module(app_name) File "/home/my_username/.virtualenvs/my_project/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module __import__(name) ImportError: No module named my_project.my_app 

Why does this error occur? How to run a script in Django with settings/dev.py instead of settings.py ?

+4
python django django-settings
Jan 30 '13 at 11:59
source share
1 answer

If you just want to run the script in django, then the easiest way to execute this is to create a ./manage.py subcommand, such as

 from django.core.management.base import BaseCommand from my_app.models import MyModel class Command(BaseCommand): help = 'runs your code in the django environment' def handle(self, *args, **options): all_entries = MyModel.objects.all() for entry in all_entries: self.stdout.write('entry "%s"' % entry) 

docs are quite useful for explaining this.

However, you can specify the settings file to run with

 $ django-admin.py runserver --settings=settings.dev 

which will start the test server using the settings in dev , however I am afraid that your problems are deeper than just that. I would not recommend modifying the manage.py file ever since this could lead to inconsistencies and future headaches.

Note that dev.py must be a complete settings file if you do. I personally would recommend this structure:

 |-settings | |- __init__.py | |- base.py | |- dev.py | |- prod.py 

and save all the general settings in base.py and change the first line of your dev.py etc. on something like

 # settings/dev.py from .base import * DEBUG = True ... 

EDIT

If you just want to check out, why not give it a try.

 $ ./manage.py shell 

or with your dev settings

 $ django-admin.py shell --settings=settings.dev 

as all OS environment variables, settings.py will be set for you, and then you can test / debug with

 >>> from my_app.models import MyModel >>> all_entries = MyModel.objects.all() >>> for entry in all_entries: ... print entry 
+7
Jan 30 '13 at 12:06 on
source share



All Articles