为什么 Environ 不在 main 以外的 Django 项目根目录中设置变量?
Why does Environ not set the variable in a Django project root other than the main?
所以我的 Django 项目根目录中有一个 .env 文件,其中包含几个变量。
当我在同一个项目根目录中使用它们时(例如在 settings.py 中),它可以工作,但是一旦我尝试访问另一个 root/module 中的变量,它就会失败:
# other root than .env
import environ
# Initialise environment variables
env = environ.Env()
environ.Env.read_env()
# Get the iCloud application password via env
mail_password = env('MAIL_PW')
# --> returns django.core.exceptions.ImproperlyConfigured: Set the MAIL_PW environment variable
然而这有效:
# same root as .env
import os
import environ
# Initialise environment variables
env = environ.Env()
environ.Env.read_env()
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY')
您的第一个代码失败,因为 environ.Env.read_env
找不到 .env
文件。
来源在此
@classmethod
def read_env(cls, env_file=None, overwrite=False, **overrides):
"""Read a .env file into os.environ.
If not given a path to a dotenv path, does filthy magic stack
backtracking to find the dotenv in the same directory as the file that
called read_env.
...
包含您的模块的目录没有 .env
文件并且 read_env
无法导入它。
您可以将 settings.py
中的所有 .env
变量导入为文档中的 suggested。然后就可以在其他模块导入设置了
settings.py
import os
import environ
# Initialise environment variables
env = environ.Env()
environ.Env.read_env()
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY')
module.py
from settings import SECRET_KEY
print(SECRET_KEY)
...
所以我的 Django 项目根目录中有一个 .env 文件,其中包含几个变量。
当我在同一个项目根目录中使用它们时(例如在 settings.py 中),它可以工作,但是一旦我尝试访问另一个 root/module 中的变量,它就会失败:
# other root than .env
import environ
# Initialise environment variables
env = environ.Env()
environ.Env.read_env()
# Get the iCloud application password via env
mail_password = env('MAIL_PW')
# --> returns django.core.exceptions.ImproperlyConfigured: Set the MAIL_PW environment variable
然而这有效:
# same root as .env
import os
import environ
# Initialise environment variables
env = environ.Env()
environ.Env.read_env()
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY')
您的第一个代码失败,因为 environ.Env.read_env
找不到 .env
文件。
来源在此
@classmethod
def read_env(cls, env_file=None, overwrite=False, **overrides):
"""Read a .env file into os.environ.
If not given a path to a dotenv path, does filthy magic stack
backtracking to find the dotenv in the same directory as the file that
called read_env.
...
包含您的模块的目录没有 .env
文件并且 read_env
无法导入它。
您可以将 settings.py
中的所有 .env
变量导入为文档中的 suggested。然后就可以在其他模块导入设置了
settings.py
import os
import environ
# Initialise environment variables
env = environ.Env()
environ.Env.read_env()
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY')
module.py
from settings import SECRET_KEY
print(SECRET_KEY)
...