无法导入模型

时间:2012-06-02 10:19:07

标签: python django

我试图执行

  
    
      

来自django.db导入模型

    
  

但它给出了以下错误.. :(

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.6/site-packages/Django-1.2.2-py2.6.egg/django/db/__init__.py", 
line 14, in <module>
if not settings.DATABASES:
File "/Library/Python/2.6/site-packages/Django-1.2.2-py2.6.egg/django/utils/functional.py",
line 276, in __getattr__
self._setup()
File "/Library/Python/2.6/site-packages/Django-1.2.2-py2.6.egg/django/conf/__init__.py", 
line 38, in _setup
raise ImportError("Settings cannot be imported, because environment variable %s is 
undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable   
DJANGO_SETTINGS_MODULE is undefined.

2 个答案:

答案 0 :(得分:3)

我假设您正在尝试从shell /终端进行此导入?如果是这样,您需要导航到Django项目目录并在执行导入之前键入python manage.py shell

答案 1 :(得分:2)

您需要在os.environ变量中添加DJANGO_SETTINGS_MODULE以指定您的设置文件。看来你试图在django应用程序之外使用django模型。你想做什么?如果您只想使用python shell进行测试,可以使用Django app dir中的Django shell:

manage.py shell --settings=settings

更新:solartic更快;)