An environment variable is a variable whose value is set outside the program, typically through functionality built into the operating system or microservice. An environment variable is made up of a name/value pair, and any number may be created and available for reference at a point in time.
The command env displays all environment variables and their values.
env file is included to use, so it's easy to have a different configuration based on the environment your app is running on. This gives you the flexibility to have different variables for local, staging, production, and even different developers' machines.
getenv() method is used to extract the value of the environment variable key if it exists. Otherwise, the default value will be returned. Note: The os module in Python provides an interface to interact with the operating system.
I use Python Dotenv Library. Just install the library pip install python-dotenv
, create a .env
file with your environment variables, and import the environment variables in your code like this:
import os
from dotenv import load_dotenv
load_dotenv()
MY_ENV_VAR = os.getenv('MY_ENV_VAR')
From the .env
file:
MY_ENV_VAR="This is my env var content."
This is the way I do when I need to test code outside my docker system and prepare it to return it into docker again.
If your system/environment/workflow supports using shell scripts, you can create a script that wraps these 2 operations:
set -a
option where "Each variable or function that is created or modified is given the export attribute and marked for export to the environment of subsequent commands".os.environ.get
codeSample .env file (config.env):
TYPE=prod
PORT=5000
Sample Python code (test.py):
import os
print(os.environ.get('TYPE'))
print(os.environ.get('PORT'))
Sample bash script (run.sh):
#!/usr/local/bin/bash
set -a
source config.env
set +a
python3 test.py
Sample run:
$ tree
.
├── config.env
├── run.sh
└── test.py
$ echo $TYPE
$ echo $PORT
$ python3 test.py
None
None
$ ./run.sh
prod
5000
When you run the Python script directly (python3 test.py
) without source
-ing the .env file, all the environ.get
calls return None
.
But, when you wrap it in a shell script that first loads the .env file into environment variables, and then runs the Python script afterward, the Python script should now be able to read the environment variables correctly.
As compared with the other popular answer, this doesn't need any external Python libraries.
This could also work for you:
env_vars = [] # or dict {}
with open(env_file) as f:
for line in f:
if line.startswith('#') or not line.strip():
continue
# if 'export' not in line:
# continue
# Remove leading `export `, if you have those
# then, split name / value pair
# key, value = line.replace('export ', '', 1).strip().split('=', 1)
key, value = line.strip().split('=', 1)
# os.environ[key] = value # Load to local environ
# env_vars[key] = value # Save to a dict, initialized env_vars = {}
env_vars.append({'name': key, 'value': value}) # Save to a list
print(env_vars)
In the comments, you'll find a few different ways to save the env vars and also a few parsing options i.e. to get rid of the leading export
keyword. Another way would be to use the python-dotenv library. Cheers.
You can use ConfigParser
. Sample example can be found here.
But this library expects your key
=value
data to be present under some [heading]
. For example, like:
[mysqld]
user = mysql # Key with values
pid-file = /var/run/mysqld/mysqld.pid
skip-external-locking
old_passwords = 1
skip-bdb # Key without value
skip-innodb
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With