9

I have environment variables that I need to get from two different files in order to keep user+pw outside of the git repo. I download the sensitive user+pass from another location and add it to .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.

env_file.env contains:

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:

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?

How can I get environment variables from two different ".env" files, both stored in the working directory?

0

4 Answers 4

14

You can add file path as an argument in load_dotenv function

from dotenv import load_dotenv
import os

load_dotenv(<file 1 path>)
load_dotenv(<file 2 path>)
Sign up to request clarification or add additional context in comments.

3 Comments

Is there anything that speaks against marking the other new answer on dotenv_values() as the answer instead?
@questionto42 If you think that answer works better for your case, I don't see any problem with that
I will wait for some votes.
7

I implemented reading env values from multiple env files by using if statements as below.

I put basic stuffs in .env file and local and prod settings in respective .env.local or .env.prod file

# Import and load environment variables
from dotenv import load_dotenv
load_dotenv() # Load .env file

# Need to check and return boolean, so check for String "True"
APP_ENVIRONMENT = os.getenv("DEBUG") == "True"

if APP_ENVIRONMENT:
    load_dotenv(".env.local")
else:
    load_dotenv(".env.prod")

3 Comments

The question was about how to read more than one env file. You make it a choice between two env files, local and prod. That is nice code as well, but it is not an answer to the question.
First it loads .env, then it loads either .env.local or .env.prod -- so it loads 2 env files and provides a good mechanism for switching between local and production.
@NicholasMcCarthy I see, then I was wrong with my remark right before yours.
3

You can call load_dotenv multiple times.

from dotenv import load_dotenv

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

for more information read this

Comments

2

If you don't need to load the env file into the environment, you can use dotenv_values to load all your files, but keep in mind that any duplicate variable will be overwritten by the last occurrence in the load order.

import os
from dotenv import dotenv_values

config = {
    **dotenv_values("env_file.env"),
    **dotenv_values("env_file_in_gitignore.env"),
}

More information on loading variables from environment or streams here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.