In this quick tutorial we will demonstrate how to load additional environment variables from file into your python application.
It loads key value pairs from a file and append it to its current runtime environment variables, so your current environment is unaffected.
python-dotenv
We will make use of the package python-dotenv so we will need to install the python package with pip:
1
|
|
The env file
I will create the .env
in my current working directory with the content:
1 2 |
|
The application
This is a basic demonstration of a python application which loads the additional environment variables from file, then we will use json.dumps(.., indent=2)
so that we can get a pretty print of all our environment variables:
1 2 3 4 5 6 7 |
|
When we run the application the output will look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
As we can see our two environment variables was added to the environment. If you would like to access your two environment variables, we can do the following:
1 2 3 4 5 6 7 8 9 |
|
And when we run that, the output should be the following:
1
|
|
Thank You
Thanks for reading, if you like my content, check out my website, read my newsletter or follow me at @ruanbekker on Twitter.