tags:

views:

26

answers:

1

Hi there,

I am facing a problem with using Django DB module in an external gateway script. I have the following python file under myproject/myapplication/lib.py

#<path>/myproject/myapplication/lib.py
from django.db import connection
from django.db import settings

#SOME METHODS ARE HERE

which is using django db module.

I need to import lib in another python script under the same directory myproject/myapplication/gateway.py

#<path>/myproject/myapplication/gateway.py
import sys, os
os.environ['DJANGO_SETTINGS_MODULE'] = "myproject.settings"
sys.path.append('../')
import lib

#SOME LOGIC HERE

but it is failing with the following error :

Fisbrok:myproject firas$ python myapplication/gateway.py
Traceback (most recent call last):
  File "myapplication/gateway.py", line 7, in ?
    import lib
  File ".... lib.py", line 1, in ?
    from django.db import connection
  File "/opt/local/lib/python2.4/site-packages/django/db/__init__.py", line 14, in ?
    if not settings.DATABASES:
  File "/opt/local/lib/python2.4/site-packages/django/utils/functional.py", line 276, in __getattr__
    self._setup()
  File "/opt/local/lib/python2.4/site-packages/django/conf/__init__.py", line 40, in _setup
    self._wrapped = Settings(settings_module)
  File "/opt/local/lib/python2.4/site-packages/django/conf/__init__.py", line 75, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'myproject.settings' (Is it on sys.path? Does it have syntax errors?): No module named myproject.settings

Am I missing something ?

A: 

your project directory, myproject has to be in python path to set the settings module as myproject.settings

you can set the project directory on python in the gateway.py file by

import sys
sys.path.insert(0, 'absolute/path/to/project')

before the line

os.environ['DJANGO_SETTINGS_MODULE'] = "myproject.settings"
Ashok