Python ImproperlyConfigured:您必须在访问设置之前定义环境变量 DJANGO_SETTINGS_MODULE 或调用 settings.configure()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26082128/
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-08-19 00:02:55  来源:igfitidea点击:

ImproperlyConfigured: You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings

pythondjangoherokudjango-settings

提问by ApathyBear

I was trying to configure my django project to deploy to heroku. I am getting the following error and I don't really know how to fix it.

我试图配置我的 django 项目以部署到 heroku。我收到以下错误,我真的不知道如何解决它。

Here is the full tracebackand error:

这是完整的回溯和错误:

22:46:15 web.1  | Traceback (most recent call last):
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495, in spawn_worker
22:46:15 web.1  |     worker.init_process()
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/workers/base.py", line 106, in init_process
22:46:15 web.1  |     self.wsgi = self.app.wsgi()
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
22:46:15 web.1  |     self.callable = self.load()
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
22:46:15 web.1  |     return self.load_wsgiapp()
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
22:46:15 web.1  |     return util.import_app(self.app_uri)
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/gunicorn/util.py", line 354, in import_app
22:46:15 web.1  |     __import__(module)
22:46:15 web.1  |   File "/Users/nir/nirla/nirla/wsgi.py", line 12, in <module>
22:46:15 web.1  |     from dj_static import Cling
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/dj_static.py", line 7, in <module>
22:46:15 web.1  |     from django.contrib.staticfiles.handlers import StaticFilesHandler as DebugHandler
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 8, in <module>
22:46:15 web.1  |     from django.contrib.staticfiles.views import serve
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/contrib/staticfiles/views.py", line 13, in <module>
22:46:15 web.1  |     from django.views import static
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/views/static.py", line 96, in <module>
22:46:15 web.1  |     template_translatable = ugettext_noop("Index of %(directory)s")
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 65, in gettext_noop
22:46:15 web.1  |     return _trans.gettext_noop(message)
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 52, in __getattr__
22:46:15 web.1  |     if settings.USE_I18N:
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
22:46:15 web.1  |     self._setup(name)
22:46:15 web.1  |   File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/conf/__init__.py", line 47, in _setup
22:46:15 web.1  |     % (desc, ENVIRONMENT_VARIABLE))
22:46:15 web.1  | 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.

Here is my wsgi.pyfile:

这是我的wsgi.py文件:

import os
from django.core.wsgi import get_wsgi_application
from dj_static import Cling

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nirla.settings") #nirla is the name of the project


application = Cling(get_wsgi_application())

And in case it was relevant, my manage.pyfile:

如果相关,我的manage.py文件:

import os
import sys

    if __name__ == "__main__":
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nirla.settings")

        from django.core.management import execute_from_command_line

        execute_from_command_line(sys.argv)

Can anyone seem to understand the issue here? And if so, can you be so kind as to explain what is wrong? Thank you!

任何人似乎都可以理解这里的问题吗?如果是这样,你能不能解释一下哪里出了问题?谢谢!

采纳答案by ApathyBear

I figured that the DJANGO_SETTINGS_MODULEhad to be set some way, so I looked at the documentation(link updated) and found:

我认为必须以某种方式设置DJANGO_SETTINGS_MODULE,所以我查看了文档(链接更新)并发现:

export DJANGO_SETTINGS_MODULE=mysite.settings

Though that is not enough if you are running a server on heroku, you need to specify it there, too. Like this:

如果您在 heroku 上运行服务器,这还不够,您还需要在那里指定它。像这样:

heroku config:set DJANGO_SETTINGS_MODULE=mysite.settings --account <your account name> 

In my specific case I ran these two and everything worked out:

在我的具体情况下,我运行了这两个,一切都解决了:

export DJANGO_SETTINGS_MODULE=nirla.settings
heroku config:set DJANGO_SETTINGS_MODULE=nirla.settings --account personal


Edit

编辑

I would also like to point out that you have to re-do this every time you close or restart your virtual environment. Instead, you should automate the process by going to venv/bin/activate and adding the line: set DJANGO_SETTINGS_MODULE=mysite.settingsto the bottom of the code. From now on every time you activate the virtual environment, you will be using that app's settings.

我还想指出,每次关闭或重新启动虚拟环境时都必须重新执行此操作。相反,您应该通过转到 venv/bin/activate 并将行:添加set DJANGO_SETTINGS_MODULE=mysite.settings到代码底部来自动化该过程。从现在开始,每次激活虚拟环境时,您都将使用该应用程序的设置。

回答by GrvTyagi

From The Definitive Guide to Django: Web Development Done Right:

The Definitive Guide to Django: Web Development Done Right

If you've used Python before, you may be wondering why we're running python manage.py shellinstead of just python. Both commands will start the interactive interpreter, but the manage.py shellcommand has one key difference: before starting the interpreter, it tells Django which settings file to use.

如果您以前使用过 Python,您可能想知道为什么我们要运行python manage.py shell而不仅仅是python. 这两个命令都会启动交互式解释器,但该manage.py shell命令有一个关键区别:在启动解释器之前,它告诉 Django 要使用哪个设置文件。

Use Case: Many parts of Django, including the template system, rely on your settings, and you won't be able to use them unless the framework knows which settings to use.

用例:Django 的许多部分,包括模板系统,都依赖于您的设置,除非框架知道要使用哪些设置,否则您将无法使用它们。

If you're curious, here's how it works behind the scenes. Django looks for an environment variable called DJANGO_SETTINGS_MODULE, which should be set to the import path of your settings.py. For example, DJANGO_SETTINGS_MODULEmight be set to 'mysite.settings', assuming mysite is on your Python path.

When you run python manage.py shell, the command takes care of setting DJANGO_SETTINGS_MODULEfor you.**

如果你很好奇,下面是它在幕后的工作原理。Django 查找名为 的环境变量DJANGO_SETTINGS_MODULE,该变量应设置为 settings.py 的导入路径。例如,DJANGO_SETTINGS_MODULE可能设置为'mysite.settings',假设 mysite 在您的 Python 路径上。

当您运行时python manage.py shell,该命令会DJANGO_SETTINGS_MODULE为您进行设置。**

回答by marcanuy

Create a .envfile that will hold your credentials at the root of your project and leave it out of versioning:

创建一个.env文件,将您的凭据保存在项目的根目录中,并将其排除在版本控制之外:

$ echo ".env" >> .gitignore

In the .env file, add the variables (adapt them according to your installation):

在 .env 文件中,添加变量(根据您的安装进行调整):

$ echo "DJANGO_SETTINGS_MODULE=myproject.settings.production"> .env
#50 caracter random key
$ echo "SECRET_KEY='####'">> .env

To use them, put this on top of your production.py settings file:

要使用它们,请将其放在 production.py 设置文件的顶部:

import os

env = os.environ.copy()
SECRET_KEY = env['SECRET_KEY']

Publish it to Heroku using this gem: http://github.com/ddollar/heroku-config.git

使用这个 gem 将它发布到 Heroku:http: //github.com/ddollar/heroku-config.git

$ heroku plugins:install git://github.com/ddollar/heroku-config.git
$ heroku config:push

This way you avoid to change virtualenv files.

这样您就可以避免更改 virtualenv 文件。

*Based on this tutorial

*基于本教程

回答by AfamO

Django needs your application-specific settings. Since it is already inside your manage.py, just use that. The faster, but perhaps temporary, solution is:

Django 需要您特定于应用程序的设置。既然它已经在你的里面了manage.py,就用它吧。更快但可能是暂时的解决方案是:

python manage.py shell

回答by Arjjun

If you are using the local server, run Django shell using python manage.py shell. It will take you to the Django python environment and you are good to go.

如果您使用的是本地服务器,请使用python manage.py shell. 它将带您进入 Django python 环境,一切顺利。