python 为什么我在 Django 中收到这个错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1641444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:47:00  来源:igfitidea点击:

Why am I getting this error in Django?

pythondjango

提问by TIMEX

I have a script that imports a models.py from an app, but it will not import! I don't believe I am supposed to manually create an "export DJANGO..." environment variable...I'm doing something else wrong.

我有一个从应用程序导入 models.py 的脚本,但它不会导入!我不相信我应该手动创建“导出 DJANGO...”环境变量...我在做其他错误的事情。

 Traceback (most recent call last):
      File "parse.py", line 8, in ?
        from butterfly.flower.models import Channel, Item
      File "/home/user/shri/butterfly/flower/models.py", line 1, in ?
        from django.db import models
      File "/usr/lib/python2.4/site-packages/django/db/__init__.py", line 10, in ?
        if not settings.DATABASE_ENGINE:
      File "/usr/lib/python2.4/site-packages/django/utils/functional.py", line 269, in __getattr__
        self._setup()
      File "/usr/lib/python2.4/site-packages/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.

采纳答案by Alex Martelli

I don't believe I am supposed to manually create an "export DJANGO..." environment variable...

我不相信我应该手动创建“导出 DJANGO...”环境变量...

Manually or otherwise, you aresupposed to ensure that variable is in the environment before you import a Django models file -- not sure what the causes are for your disbelief, but, whatever they may be, that disbelief is ill-founded.

手动或以其他方式,你应该确保变量是在环境导入前的Django模型文件-而不是原因是知道什么你不相信,但是,无论是什么,那是怀疑不攻自破。

回答by Paul McMillan

You need to properly import your settings file. If you don't import the django environment variables first, your import will fail as you've noticed. You need to code something like:

您需要正确导入您的设置文件。如果您不先导入 django 环境变量,您的导入将会失败,正如您所注意到的。您需要编写如下代码:

import os

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "myproject.settings"

#do your stuff

回答by Ian

from command line ran "python manage.py shell" should include the right settings for you and give you the python prompt.

从命令行运行“python manage.py shell”应该包括适合您的设置并为您提供python提示。

回答by Carl Meyer

One alternative to messing about with the environment is to just write your script as a Django custom management command, which takes care of setting up the environment for you (along with other things like option parsing, etc).

搞乱环境的一种替代方法是将您的脚本编写为Django 自定义管理命令,它负责为您设置环境(以及选项解析等其他内容)。

回答by Vijesh Venugopal

From command line, please execute this command. This may help you:

从命令行,请执行此命令。这可能会帮助您:

export DJANGO_SETTINGS_MODULE=projectname.settings

回答by AlvaroAV

I had this similar error, and I realized that I forgot to create the file "init.py" in to the app folder:

我遇到了类似的错误,我意识到我忘记在 app 文件夹中创建文件“ init.py”:

myapp:
    manage.py
    myapp:
        __init__.py
        settings.py
        ....