Python: Pass environment variables as a configuration file

Today, I got a task to pass all environment variables as a configuration files. It will make it easy to pass different parameters based on the running environment (such as development or production). I used “python-dotenv” python library to do this task. The below code snippet shows how you can do it in your program.

env_path = 'path to file'
load_dotenv(dotenv_path=env_path)
# now you can load the environment variables
greeting = os.getenv('greeting', '')
name = os.getenv('name', 'default')

The configuration file looks like this
greeting=hello
name=world!

Thank you for reading.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s