Configuring eulexistdb with python bringing errors in django setting module

Question:

I have following code written in python in order to communicate with ExistDB using eulexistdb module.

from eulexistdb import db    
class TryExist:    
    def __init__(self):
        self.db = db.ExistDB(server_url="http://localhost:8899/exist")    
    def get_data(self, query):
        result = list()
        qresult = self.db.executeQuery(query)
        hits = self.db.getHits(qresult)
        for i in range(hits):
            result.append(str(self.db.retrieve(qresult, i)))
        return result

query = '''
let $x:= doc("/db/sample/books.xml")
return $x/bookstore/book/author/text()
'''
a = TryExist()
response = a.get_data(query)
print response

I am amazed that this code runs fine in Aptana Studio 3 giving me the output I want, but when running from other IDE or using command "python.exe myfile.py" brings following error:

django.core.exceptions.ImproperlyConfigured: Requested setting EXISTDB_TIMEOUT, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

I used my own localsetting.py to solve the problem using following code:

import os
# must be set before importing anything from django
os.environ['DJANGO_SETTINGS_MODULE'] = 'localsettings'
... writing link for existdb here...

Then I get error as:

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

How do I configure the setting in Django to suit for ExistDB? Help me here please..

Asked By: Pant

||

Answers:

Never Mind. I found the answer with little research from this site. What I did was created a localsetting.py file with following configurations.

EXISTDB_SERVER_USER = 'user'
EXISTDB_SERVER_PASSWORD = 'admin'
EXISTDB_SERVER_URL = "http://localhost:8899/exist"
EXISTDB_ROOT_COLLECTION = "/db"

and in my main file myfile.py I used :

from localsettings import EXISTDB_SERVER_URL
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'localsettings.py'

and In the class TryExist I changed in __ init __() as:

def __init__(self):
        self.db = db.ExistDB(server_url=EXISTDB_SERVER_URL)

PS: Using only os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘localsettings’ brings the django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty..

Answered By: Pant

The reason your code works in an IDE but not at the command line is probably that you have a difference in what Python environments are used to run your code.

I’ve done a couple of tests:

  1. Virtualenv with eulexistdb installed but not Django. eulexistdb tries to load django.conf but fails and so does not try to get its configuration from a Django configuration. Ultimately, your code runs without error.

  2. Virtualenv with ‘eulexistdb*and* Django:eulexistdbtries to loaddjango.conf` and succeed. I then tries to get is configuration from the Django configuration but fails. I get the same error you describe in your question.

To prevent the error in the presence of a Django installation, the problem can be fixed by adding a Django configuration like you did in your accepted self-answer. But if the code you are writing does not otherwise use Django, that’s a bit of a roundabout way to get your code to run. The most direct way to fix the problem is to simply add a timeout parameter to the code that creates the ExistDB instance:

    self.db = db.ExistDB(
        server_url="http://localhost:8080/exist", timeout=None)

If you do this, then there won’t be any error. Setting the timeout to None leaves the default behavior in place but prevents eulexistdb from looking for a Django configuration.

Answered By: Louis
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.