gpt4 book ai didi

python - 为什么我在 Django 中收到此错误?

转载 作者:IT老高 更新时间:2023-10-28 20:42:30 29 4
gpt4 key购买 nike

我有一个从应用程序导入 models.py 的脚本,但它不会导入!我不相信我应该手动创建一个“export 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.

最佳答案

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

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

关于python - 为什么我在 Django 中收到此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1641444/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com