[英]Reading environment variables from more than one ".env" file in Python
I have environment variables that I need to get from two different files in order to keep user+pw outside of the git repo.我需要从两个不同的文件中获取环境变量,以便将 user+pw 保留在 git 存储库之外。 I download the sensitive user+pass from another location and add it to.gitignore.
我从另一个位置下载敏感用户+密码并将其添加到.gitignore。
I am using我在用
from os import getenv
from dotenv import load_dotenv
...
load_dotenv()
DB_HOST=getenv('DB_HOST') # from env file 1
DB_NAME=getenv('DB_NAME') # from env file 1
DB_USER=getenv('DB_USER') # from env file 2
DB_PASS=getenv('DB_PASS') # from env file 2
and I have the two ".env" files in the folder of the python script.我在 python 脚本的文件夹中有两个“.env”文件。
env_file.env contains: env_file.env 包含:
DB_HOST=xyz
DB_NAME=abc
env_file_in_gitignore.env which needs to stay out of the git repo but is available by download using an sh script: env_file_in_gitignore.env 需要远离 git 存储库,但可以使用 sh 脚本下载:
DB_USER=me
DB_PASS=eao
How to avoid the error:如何避免错误:
TypeError: connect() argument 2 must be str, not None
connect() argument 2 must be str, not None
which is thrown since one of the two files are not used for the.env import?由于两个文件之一未用于 .env 导入,因此抛出了什么?
How can I get environment variables from two different ".env" files, both stored in the working directory?如何从存储在工作目录中的两个不同的“.env”文件中获取环境变量?
You can add file path as an argument in load_dotenv
function您可以在
load_dotenv
function 中添加文件路径作为参数
from dotenv import load_dotenv
import os
load_dotenv(<file 1 path>)
load_dotenv(<file 2 path>)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.