4

I have a xml parser that will store that data into a MySQL database, as part of a django app. The parser is intended to run daily, capturing some tests outputs:

`dbsync.py`:

   1 #!/usr/bin/env python
   2 import os
   3 os.environ['DJANGO_SETTINGS_MODULE'] = 'autotester.settings'
   4 
   5 import xml_parser
   6 from models import *

The Django project is called autotester and the app is called autoreporter. When I execute

python dbsync.py

I'm getting:

Traceback (most recent call last):
  File "autoreporter/dbsync.py", line 6, in <module>
    from models import *
  File "/root/autotester/autoreporter/models.py", line 1, in <module>
    from django.db import models
  File "/usr/lib/python2.7/dist-packages/django/db/models/__init__.py", line 5, in <module>
    from django.db.models.query import Q
  File "/usr/lib/python2.7/dist-packages/django/db/models/query.py", line 17, in <module>
    from django.db.models.deletion import Collector
  File "/usr/lib/python2.7/dist-packages/django/db/models/deletion.py", line 4, in <module>
    from django.db.models import signals, sql
  File "/usr/lib/python2.7/dist-packages/django/db/models/sql/__init__.py", line 4, in <module>
    from django.db.models.sql.subqueries import *
  File "/usr/lib/python2.7/dist-packages/django/db/models/sql/subqueries.py", line 12, in <module>
    from django.db.models.sql.query import Query
  File "/usr/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 22, in <module>
    from django.db.models.sql import aggregates as base_aggregates_module
  File "/usr/lib/python2.7/dist-packages/django/db/models/sql/aggregates.py", line 9, in <module>
    ordinal_aggregate_field = IntegerField()
  File "/usr/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 116, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "/usr/lib/python2.7/dist-packages/django/conf/__init__.py", line 54, in __getattr__
    self._setup(name)
  File "/usr/lib/python2.7/dist-packages/django/conf/__init__.py", line 49, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/lib/python2.7/dist-packages/django/conf/__init__.py", line 132, in __init__
    % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'autotester.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named autotester.settings

How can I have DJANGO_SETTINGS_MODULE set, so I can execute the dbsync.py script properly?

5
  • Where is the script located in regards to your project structure? Commented May 7, 2015 at 20:54
  • One problem is probably you do not have autotester.settings in the PYTHONPATH Commented May 7, 2015 at 21:25
  • 1
    Why not make the script a Django management command? That would let the framework take care of all of this. Commented May 8, 2015 at 10:25
  • @Joe this sounds like a great idea is there a way you can post a answer as an example ? Commented May 8, 2015 at 20:27
  • @cmidi I haven't written much of the script ... that's all the script is actually Commented May 8, 2015 at 20:41

2 Answers 2

1

Two issues

  1. You need to import models from your app, so provided that your Django project is in the PYTHONPATH you can simply import the app models by:

from autoreporter.models import *

or do a relative import, if dbsync.py(looks from your traceback) is in the app directory.

from .models import *
  1. Add Django project to PYTHONPATH. You need to have the Django project in the PYTHONPATH to it be accessed from your dbsync.py(provided it is in your django app directory), quick easy fix for this would be to do the following in your code to dbsync.py.

    import sys
    import os
    ##get project directory
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    ##get project parent directory and add to python path using sys.path.append
    SYS_PATH = os.path.dirname(BASE_DIR)
    if SYS_PATH not in sys.path:
        sys.path.append(SYS_PATH)
    os.environ['DJANGO_SETTINGS_MODULE'] = 'autotester.settings'
    
Sign up to request clarification or add additional context in comments.

Comments

1

I run a few scripts using the ORM without the rest of Django. I wouldn't set the environment in the script.

Since Django 1.7 you need to import your models, and run django setup.

I would set the environment as you do for the main Django application. Then at the start of your script add these lines. Those should set up the Django environment so it can access your models the same way a standard Django app will.

import django
django.setup()

import xml_parser
from .models import *

2 Comments

would this work if the script is not present in the app directory ? I see you are doing a relative import of the model using .model ?
You would have to run it with the working directory in the same place as manage.py, so that the settings file gets imported correctly. But the script can be anywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.