简体   繁体   English

从 Python 中的多个“.env”文件中读取环境变量

[英]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>)

there is a method load env file is load_dotenv you can use as many env file you want using有一种方法加载 env 文件是load_dotenv ,您可以使用任意数量的 env 文件

from dotenv import load_dotenv

load_dotenv('path1')
load_dotenv('path2)
...

for more information read this有关更多信息,请阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 pythonanywhere.com 中的 .env 文件读取环境变量 - Reading environment variables from .env file in pythonanywhere.com 在Python中读取具有多个空格作为分隔符的txt文件 - Reading txt file with more than one space as a delimiter in Python 读取python中的txt文件,观察之间有多个空格 - reading a txt file in python with more than one space between the observation 从环境文件中读取环境变量 - Reading in environment variables from an environment file 使用python从JSON读取多个属性值 - Reading more than one attribute value from JSON using python Python WSGI:不止一次读取env [&#39;wsgi.input&#39;] - Python WSGI: Reading env['wsgi.input'] more than once 如何将多个python变量传递到正则表达式中,以便从文本文件中查找行? - How to pass more than one python variables into regex insearch of finding lines from a text file? 使用 Python 和 dotenv 更改保存在 .env 文件中的环境变量 - Change Environment Variables Saved In .env File With Python and dotenv 可以有一个 .env 文件并根据 python 环境选择变量吗? - It is possible to have a single .env file and pick variables based on the python environment? Python如果N个变量中有多个是真的 - Python if more than one of N variables is true
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM