django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are notb

本文详细解析了Django项目中因未正确配置环境变量导致的ImproperlyConfigured错误,并提供了具体的解决步骤,包括如何设置DJANGO_SETTINGS_MODULE及调用django.setup()方法,确保应用程序能正常识别数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

报错: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.
大意是:异常,没有正确配置:请求设置已安装的应用程序,但未配置设置。必须定义环境变量DJANGO_SETTINGS模块或调用设置.配置并在访问设置之前。
在这里插入图片描述
原因是由于这个模块并没有识别数据库,虽然我已经把database添加到setting。develop文件中了,但是还是需要在文件开头添加两行配置环境变量的配置语句,让程序知道该去哪儿寻找 models 中的文件。添加如下代码:

import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings")# project_name 项目,须是配置数据库的模块
django.setup()

这个项目中我的设置如下图:
在这里插入图片描述

运行py文件报错:/usr/bin/python3 /Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/pydevd.py --multiprocess --qt-support=auto --client 127.0.0.1 --port 64866 --file /Users/shenxin/Desktop/176All/project/jiaoben/scriptproject/great/takealot_review250609amz/takealot_review_20250609amz.py AmazonCategories_20241018.csv _tal AmazonCategories_20241018_1.csv > outputamz.log 2>&1 & Connected to pydev debugger (build 223.8836.43) /Users/shenxin/Desktop/176All/project/jiaoben/scriptproject/great/takealot_review250609amz/takealot_review_20250609amz.py:1: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses import imp /Library/Python/3.9/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020 warnings.warn( Traceback (most recent call last): File "<frozen importlib._bootstrap>", line 1007, in _find_and_load File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 680, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 850, in exec_module File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed File "/Library/Python/3.9/site-packages/emoji/__init__.py", line 1, in <module> from .models import Emoji as emoji_class File "/Library/Python/3.9/site-packages/emoji/models.py", line 14, in <module> from . import settings File "/Library/Python/3.9/site-packages/emoji/settings.py", line 5, in <module> EMOJI_IMG_TAG = getattr(settings, 'EMOJI_IMG_TAG', ( File "/Users/shenxin/Library/Python/3.9/lib/python/site-packages/django/conf/__init__.py", line 79, in __getattr__ self._setup(name) File "/Users/shenxin/Library/Python/3.9/lib/python/site-packages/django/conf/__init__.py", line 60, in _setup raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Requested setting EMOJI_IMG_TAG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. python-BaseException Traceback (most recent call last): File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_xml.py", line 161, in _get_type def _get_type(self, o, type_object, type_name): File "/Users/shenxin/Library/Python/3.9/lib/python/site-packages/django/utils/functional.py", line 254, in inner def inner(self, *args): File "/Users/shenxin/Library/Python/3.9/lib/python/site-packages/django/conf/__init__.py", line 51, in _setup def _setup(self, name=None): django.core.exceptions.ImproperlyConfigured: Requested settings, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. Process finished with exit code 1
最新发布
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值