简体   繁体   English

django.core.exceptions.ImproperlyConfigured:请求设置 USE_I18N,但未配置设置

[英]django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N, but settings are not configured

I want to connect MySQL database to my django project, but it is throwing an error:我想将 MySQL 数据库连接到我的 django 项目,但出现错误:

"django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings." “django.core.exceptions.ImproperlyConfigured:请求设置 USE_I18N,但未配置设置。您必须在访问设置之前定义环境变量 DJANGO_SETTINGS_MODULE 或调用 settings.configure()。”

Trace:痕迹:

 (myenv) LIBINGLADWINs-MacBook-Air:libinrenold$ django-admin dbshell
Traceback (most recent call last):
  File "/Users/libinrenold/Desktop/djangoworks/myenv/bin/django-admin", line 11, in <module>
    sys.exit(execute_from_command_line())
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/core/management/base.py", line 322, in execute
    saved_locale = translation.get_language()
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/utils/translation/__init__.py", line 195, in get_language
    return _trans.get_language()
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/utils/translation/__init__.py", line 59, in __getattr__
    if settings.USE_I18N:
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/Users/libinrenold/Desktop/djangoworks/myenv/lib/python3.6/site-packages/django/conf/__init__.py", line 39, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

settings.py .设置.py

  DATABASES = {
        'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'user',
        'PASSWORD': 'root',
        'HOST':'',
        'PORT': '',
        }
     }

You must define the relevant variable to show where you settings.py file lives:您必须定义相关变量以显示settings.py文件所在的位置:

export DJANGO_SETTINGS_MODULE=mysite.settings

This is the relevant docs entry:这是相关的文档条目:

When you use Django, you have to tell it which settings you're using.当您使用 Django 时,您必须告诉它您正在使用哪些设置。 Do this by using an environment variable, DJANGO_SETTINGS_MODULE.通过使用环境变量 DJANGO_SETTINGS_MODULE 来执行此操作。

The value of DJANGO_SETTINGS_MODULE should be in Python path syntax, eg mysite.settings. DJANGO_SETTINGS_MODULE 的值应该是 Python 路径语法,例如 mysite.settings。 Note that the settings module should be on the Python import search path.请注意,设置模块应位于 Python 导入搜索路径上。

If you are using a virtualenv (which is the best practice), you can paste the relevant export command in the file <path-to-virtualenv>/bin/activate如果您使用的是virtualenv (这是最佳实践),您可以将相关export命令粘贴到文件<path-to-virtualenv>/bin/activate

It happens to me too, I just now found unintentional.我也遇到过,我现在才发现是无意的。

I changed "configuration" in top of PyCharm IDE so pycharm confused when try to run django code.我在 PyCharm IDE 顶部更改了“配置”,因此在尝试运行 django 代码时 pycharm 感到困惑。 Exactly the problem popup when I try to run server with shortcut but when I used terminal to run server, I found django code not having any problem and pycharm can't found setting.正是当我尝试使用快捷方式运行服务器时弹出的问题,但是当我使用终端运行服务器时,我发现 django 代码没有任何问题并且 pycharm 找不到设置。

So please check "configuration" in top pycharm's ide maybe you do same mistake :)因此,请检查顶部 pycharm 的 ide 中的“配置”,也许您会犯同样的错误:)

I'm new in django so be calm if my answer was silly.我是 Django 新手,所以如果我的回答很愚蠢,请保持冷静。

配置图片

put this in top of settings.py this will configure django for you把它放在 settings.py 的顶部,这将为你配置 django

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", __file__)
import django
django.setup()

I had the same error with Django 3.0.8 (july 2020) when trying to run the server, and in my case no need for the DJANGO_SETTINGS_MODULE environment variable.尝试运行服务器时,我在 Django 3.0.8(2020 年 7 月)上遇到了同样的错误,在我的情况下,不需要 DJANGO_SETTINGS_MODULE 环境变量。 The solution is to use another form to run the server:解决方案是使用另一种形式运行服务器:

cd <directory containing manage.py>
python3 manage.py runserver

It works to run Django shell too :它也可以运行 Django shell:

python3 manage.py shell

Like raratiru answered, you need DJANGO_SETTINGS_MODULE environment variable defined with the relative pythonic path to your setting file.就像 raratiru 回答的那样,您需要使用设置文件的相对 pythonic 路径定义DJANGO_SETTINGS_MODULE环境变量。

OR use your django-admin command with the settings parameter:或者使用带有settings参数的django-admin命令

django-admin --settings=mysite.settings dbshell

Problem问题

I had the same error with Django==3.2 and djangorestframework==3.12.4 (2021/04) when I ran unittest.当我运行 unittest 时,我在 Django==3.2 和 djangorestframework==3.12.4 (2021/04) 上遇到了同样的错误。 In my case, the python manage.py test works properly but I cannot directly run or debug the test module which was in a specific app, and I get this error:就我而言, python manage.py test工作正常,但我无法直接运行或调试特定应用程序中的测试模块,并且出现此错误:

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.

Solution解决方案

First I added the django project root to the sys.path .首先,我将 django 项目根添加到sys.path Then I added DJANGO_SETTINGS_MODULE to the environment variable to address the project setting root and called the Django setup function.然后我在环境变量中添加了DJANGO_SETTINGS_MODULE来寻址项目设置根,调用Django setup函数。 This is my code:这是我的代码:

from os import path, environ
from sys import path as sys_path
from django import setup

sys_path.append(<path to django setting.py>)    
environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_project.settings')
setup()

I had this problem because I tried to import a django script after just typing我遇到了这个问题,因为我在输入后尝试导入 django 脚本

python

instead of代替

python manage.py shell

Duh!呸!

终端中的以下代码手动通知我正在使用的设置对我有用:

set DJANGO_SETTINGS_MODULE=mysite.settings

Dear all I faced the same problem and i scrapped the web to find a solution.亲爱的所有人,我遇到了同样的问题,我放弃了 web 以找到解决方案。 None of the one above solved anything.以上都没有解决任何问题。

The issue in my case was related to a wrong configuration of pycharm. As for someone above the app didnt have any issue when launched from the command line/ shell.在我的案例中,问题与 pycharm 的错误配置有关。至于上面的人,从命令行/shell 启动应用程序时没有任何问题。

The issue is here:问题在这里:

在此处输入图像描述

For some reason the env variable disappeared.由于某种原因,env 变量消失了。 i added back and everything started to work again without any issue.我加回去,一切都重新开始工作,没有任何问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 raise ImproperlyConfigured(django.core.exceptions.ImproperlyConfigured:请求设置 INSTALLED_APPS,但未配置设置 - raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured django.core.exceptions.ImproperlyConfigured 错误:请求设置 INSTALLED_APPS,但未配置设置 - django.core.exceptions.ImproperlyConfigured error: Requested setting INSTALLED_APPS, but settings are not configured django.core.exceptions.ImproperlyConfigured:请求设置INSTALLED_APPS,但未配置设置 - django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured django.core.exceptions.ImproperlyConfigured:请求的设置DEFAULT_INDEX_TABLESPACE,但未配置设置 - django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured django.core.exceptions.ImproperlyConfigured:请求设置 AUTH_USER_MODEL,但未配置设置 - django.core.exceptions.ImproperlyConfigured: Requested setting AUTH_USER_MODEL, but settings are not configured django.core.exceptions.ImproperlyConfigured:请求的设置DEFAULT_INDEX_TAB LESPACE,但未配置设置 - django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TAB LESPACE, but settings are not configured django.core.exceptions.ImproperlyConfigured:请求设置 LOGGING_CONFIG,但未配置设置 - django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured django.core.exceptions.ImproperlyConfigured:请求设置 INSTALLED_APPS,但未配置设置。 请帮帮我 - django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. plz help me out plz django.core.exceptions.ImproperlyConfigured:请求设置CACHES,但未配置设置。 您必须定义环境变量 - django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment varia django.core.exceptions.ImproperlyConfigured:请求设置 LOGGING_CONFIG - django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM