我尝试将环境变量从名为.env的文件加载到settings.py文件 在这里,我创建了.env文件和设置文件相同的文件夹。
这是我的.env文件
header = col("columna") >> colsep >> col("columnb") >> colsep >> column("columnc") >> eol >> eoi;
这是我的setting.py文件
DEBUG=on
SECRET_KEY=ksmdfw3324@#jefm
DATABASE_URL=psql://urser:un-githubbedpassword@127.0.0.1:8458/database
SQLITE_URL=sqlite:///my-local-sqlite.db
CACHE_URL=memcache://127.0.0.1:11211,127.0.0.1:11212,127.0.0.1:11213
REDIS_URL=rediscache://127.0.0.1:6379/1?
client_class=django_redis.client.DefaultClient&password=ungithubbed-secret
MYSQL_DATABASE = student
MYSQL_USERNAME = root
SECRET_KEY=secret-key
我已安装import os
from os.path import join, dirname
from dotenv import load_dotenv
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
# Accessing variables.
dbname = os.getenv('MYSQL_DATABASE')
secret_key = os.getenv('SECRET_KEY')
# Using variables.
print(dabname)
print(secret_key)
问题是我无法在设置文件中获取环境变量
尝试pip install -U python-dotenv
时遇到此错误
python manage.py runserver
我不确定如何创建开发和生产环境变量以及有关此嵌入式null字符的信息。请帮助我任何人 预先感谢
编辑:我现在将.env文件保存到内部设置
C:\Users\mehul\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\dotenv\main.py:65: UserWarning: File doesn't exist
warnings.warn("File doesn't exist {}".format(self.dotenv_path))
Traceback (most recent call last):
File "manage.py", line 28, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\mehul\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\django\core\management\__init__.py", line 371, in
execute_from_command_line
utility.execute()
File "C:\Users\mehul\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\django\core\management\__init__.py", line 317, in execute
settings.INSTALLED_APPS
File "C:\Users\mehul\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\django\conf\__init__.py", line 56, in __getattr__
self._setup(name)
File "C:\Users\mehul\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\django\conf\__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module)
File "C:\Users\mehul\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\django\conf\__init__.py", line 106, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Users\mehul\AppData\Local\Programs\Python\Python36-
32\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\xampp\htdocs\epitastudent\epitastudent\settings.py", line 25, in
<module>
load_dotenv()
File "C:\Users\mehul\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\dotenv\main.py", line 255, in load_dotenv
return DotEnv(f,
verbose=verbose).set_as_environment_variables(override=override)
File "C:\Users\mehul\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\dotenv\main.py", line 98, in set_as_environment_variables
os.environ[k] = v
File "C:\Users\mehul\AppData\Local\Programs\Python\Python36-32\lib\os.py",
line 675, in __setitem__
self.putenv(key, value)
ValueError: embedded null character
如何区分开发环境凭证和生产凭证
答案 0 :(得分:2)
尝试以下方法:
import os
import environ
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
environ.Env.read_env(env_file=os.path.join(BASE_DIR, '.env'))
答案 1 :(得分:2)
对于未来的 Google 员工,这里有另一种方法。在 manage.py
中添加:
from dotenv import load_dotenv
load_dotenv()
现在,您可以在项目的其他任何位置(包括 settings.py
)执行以下操作以访问环境变量:
import os
os.environ.get("NAME")
答案 2 :(得分:1)
我无法在Python 3.5版中使用load_dotenv()加载环境变量-更高版本可以正常工作。
解决方法是在路径中明确包含包含.env的文件夹。因此,假设文件夹 project 包含.env,则settings.py将包含以下几行:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
from dotenv import load_dotenv
load_dotenv(os.path.join(BASE_DIR, "project", ".env"))
答案 3 :(得分:0)
我不太确定,但是可能与.env文件的路径有关。也许您应该尝试在settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(文件)))
dotenv_path = os.path.join(BASE_DIR,file.env)
答案 4 :(得分:0)
我认为主要有两个软件包可以使用
pip install django-environ
和
pip install python-dotenv
我选择使用dotenv,因为django-environ给了我一些错误。
Error: The SECRET_KEY setting must not be empty
以下是我的工作解决方案,请注意在调用os.environ时它是方括号[]
。
我的版本是Django == 2.2.6,python == 3.7.5
settings.py
import os
from dotenv import load_dotenv
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
load_dotenv(os.path.join(BASE_DIR, '.env'))
SECRET_KEY = os.environ['SECRET_KEY']
.env文件使用
存储在当前目录中.env
export SECRET_KEY="xxxxxx"
export DB_NAME = "xxx"