DJANGO_SETTINGS_MODULE Visual Studio 代码终端错误

Posted

技术标签:

【中文标题】DJANGO_SETTINGS_MODULE Visual Studio 代码终端错误【英文标题】:DJANGO_SETTINGS_MODULE Visual Studio Code Terminal Error 【发布时间】:2021-04-10 12:38:46 【问题描述】:

根据其他帖子,我在我的应用程序中添加了一个新的 .env 文件,但此错误仍然出现。我还在我的 settings.py 文件中添加了顶部设置模块,但仍然没有运气。对造成这种情况的原因有什么想法吗?

项目路径(db 和 manage.py 存在的地方):C:\Users\xxx\Python\Price Tracking\Django\mysite

Visual Studio 代码终端错误:

Traceback (most recent call last):
  File "c:/xxxx/Python/Price Tracking/Django/mysite/polls/models.py", line 7, in <module>
    class Question(models.Model):
  File "C:\Python38\lib\site-packages\django\db\models\base.py", line 107, in __new__
    app_config = apps.get_containing_app_config(module)
  File "C:\Python38\lib\site-packages\django\apps\registry.py", line 252, in get_containing_app_config
    self.check_apps_ready()
  File "C:\Python38\lib\site-packages\django\apps\registry.py", line 134, in check_apps_ready
    settings.INSTALLED_APPS
  File "C:\Python38\lib\site-packages\django\conf\__init__.py", line 76, in __getattr__
    self._setup(name)
  File "C:\Python38\lib\site-packages\django\conf\__init__.py", line 57, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
PS C:\Users\xxxx\Python\Price Tracking\Django\mysite>

错误 2

Traceback (most recent call last):
  File "c:/Users/xxx/Python/Price Tracking/Django/mysite/mysite/settings.py", line 19, in <module>
    django.setup()
  File "C:\Python38\lib\site-packages\django\__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "C:\Python38\lib\site-packages\django\conf\__init__.py", line 76, in __getattr__
    self._setup(name)
  File "C:\Python38\lib\site-packages\django\conf\__init__.py", line 63, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Python38\lib\site-packages\django\conf\__init__.py", line 142, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\xxxx\Python\Price Tracking\Django\mysite\mysite\settings.py", line 19, in <module>
    django.setup()
  File "C:\Python38\lib\site-packages\django\__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "C:\Python38\lib\site-packages\django\conf\__init__.py", line 76, in __getattr__
    self._setup(name)
  File "C:\Python38\lib\site-packages\django\conf\__init__.py", line 63, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Python38\lib\site-packages\django\conf\__init__.py", line 161, in __init__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

设置

import os
import sys
sys.path.append(r"C:\Users\xxxx\Python\Price Tracking\Django\mysite")
os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"
import django
django.setup()
from django.core.management import call_command

from pathlib import Path

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '**=6fb4%z!pxp*ce8$kvj+z2%98)jo&hv!*3*m^wxxxxxxxxxxxxxx'

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_plotly_dash.apps.DjangoPlotlyDashConfig',
    'chartjs',
]

C:\Users\xxxx\Django\mysite.env 文件:

DJANGO_SETTINGS_MODULE='mysite.settings'

【问题讨论】:

似乎可以帮助您解决问题。 ***.com/questions/29828019/… 这里不走运...我按照这些步骤操作,现在收到错误 ModuleNotFoundError: No module named 'mysite'。我已经更新了上面的代码 我不太了解您的所有项目架构。那么你现在试试这个怎么样:***.com/questions/48043853/… 无论出于何种原因,现在嵌入了两个 mysite 文件夹,因此我调整了代码以在末尾添加一个 mysite 并且该部分现在正在工作......它引发的下一个错误是 django.core .exceptions.ImproperlyConfigured:SECRET_KEY 设置不能为空。即使我的设置文件中列出了一个密钥(见上文) 我不知道你为什么一个接一个地得到这么多错误。所有这些都是突然冒出来的,还是你最近做了一些改变。也许解决方案要容易得多。如果你不知道,也请检查一下:***.com/questions/19128540/… 【参考方案1】:

在脚本顶部添加这些行,顺序很重要。

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

import django
django.setup()

from django.core.management import call_command

For more detail click here..

【讨论】:

这会产生错误:SECRET_KEY 设置不能为空。但我的 settings.py 中确实列出了一个密钥 @RCarmody 告诉我你的全部错误或提出这个问题。

以上是关于DJANGO_SETTINGS_MODULE Visual Studio 代码终端错误的主要内容,如果未能解决你的问题,请参考以下文章

使用 DJANGO_SETTINGS_MODULE 时如何导入设置?

Djang DJANGO_SETTINGS_MODULE

DJANGO_SETTINGS_MODULE 未命名模块

DJANGO_SETTINGS_MODULE - 未加载指定的设置文件?

Django“未配置设置”错误,无法定义环境变量 DJANGO_SETTINGS_MODULE

django.setup(): ModuleNotFoundError: 设置 DJANGO_SETTINGS_MODULE 时没有名为“api.apps”的模块