2010-07-07 76 views
0

我在外部網關腳本中使用Django DB模塊時遇到問題。 我有下 myproject的以下Python文件/所有MyApplication/lib.py由於設置模塊加載,Django DB模塊的外部使用失敗

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

#SOME METHODS ARE HERE 
,其使用Django分貝模塊

我需要進口LIB在另一個python腳本同一目錄下的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 

,但它與下面的錯誤而失敗:

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 

我錯過了什麼嗎?

回答

1

您的項目目錄,myproject必須是在python路徑來進行設置模塊myproject.settings

您可以通過

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

設置在gateway.py文件蟒項目目錄line

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