英文:
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, '.env'))
SECRET_KEY = env('SECRET_KEY')
DEBUG = env('DEBUG')
# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
DATABASES = {
"default": 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 "C:\repos\DWL_backend\zijn\settings.py", line 19, in <module>
env = environ.Env()
AttributeError: module 'environ' has no attribute 'Env'
and I installed already the module
django-environ = "*"
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论