AttributeError: 模块’environ’没有属性’Env’

huangapple go评论65阅读模式
英文:

AttributeError: module 'environ' has no attribute 'Env'

问题

我有一个Django应用程序,我尝试使用environ模块。

当然,我在这个错误上搜索了很多。但我无法弄清楚设置上有什么问题。

所以这是我的settings.py文件的一部分:

from pathlib import Path
import os
import environ

BASE_DIR = Path(__file__).resolve().parent.parent

env = environ.Env()
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))

SECRET_KEY = env('SECRET_KEY')

DEBUG = env('DEBUG')

# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

DATABASES = {
    "default": env.db(),
}

.env文件看起来像这样:

DEBUG=on

但是,例如,当我尝试运行应用程序时:

python manage.py runserver 192.168.1.135:8000

我得到了这个错误:

File "C:\repos\DWL_backend\zijn\settings.py", line 19, in <module>
    env = environ.Env()
AttributeError: module 'environ' has no attribute 'Env'

而且我已经安装了模块:

django-environ="*"

问题:如何解决这个错误?

英文:

I have a django app. And I try to use the module environ.

Of course I googled a lot on this error. But I can't figure out what is wrong whith the setup.

So this is part of my settings.py file:

from pathlib import Path
import os
import environ


BASE_DIR = Path(__file__).resolve().parent.parent

env = environ.Env()
environ.Env.read_env(os.path.join(BASE_DIR, &#39;.env&#39;))

SECRET_KEY = env(&#39;SECRET_KEY&#39;)

DEBUG = env(&#39;DEBUG&#39;)

# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

DATABASES = {
    &quot;default&quot;: env.db(),
}

and .env file looks like:

DEBUG=on

but for exmaple when I try to run the app with:

python manage.py runserver 192.168.1.135:8000

I get this error:

File &quot;C:\repos\DWL_backend\zijn\settings.py&quot;, line 19, in &lt;module&gt;
    env = environ.Env()
AttributeError: module &#39;environ&#39; has no attribute &#39;Env&#39;

and I installed already the module

django-environ = &quot;*&quot;

Question: how to resolve this error?

答案1

得分: 1

你可能安装了错误的模块。我的IDE建议使用pip install environ,但这是一个不同的包,是一个错误的建议。你需要安装的是pip install django-environ。运行pip freeze,确保你安装了django-environ,而不是其他的。

英文:

You've probably installed the wrong module. My IDE suggested pip install environ, which is a different package and a wrong suggestion. The one you need to install here is pip install django-environ. Run pip freeze and make sure you have the django-environ, not the other one.

huangapple
  • 本文由 发表于 2023年7月17日 23:47:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定