Skip to content

Instantly share code, notes, and snippets.

@radzhome
Last active Aug 17, 2018
Embed
What would you like to do?
python read env file
env_file='/path/to/env/file'
env_vars = []
with open(env_file) as f:
for line in f:
if line.startswith('#'):
continue
# if 'export' not in line:
# continue
# Remove leading `export `
# then, split name / value pair
# key, value = line.replace('export ', '', 1).strip().split('=', 1)
key, value = line.strip().split('=', 1)
# os.environ[key] = value
env_vars.append({'name': key, 'value': value})
print(env_vars); input()
import os
def get_env(env_file='.env', set_environ=True):
"""
Set env vars from a file
:param env_file:
:param set_environ:
:return: list of tuples, env vars
"""
env_vars = []
with open(env_file) as f:
for line in f:
if line.startswith('#'):
continue
# Remove leading `export `
if line.lower().startswith('export '):
key, value = line.replace('export ', '', 1).strip().split('=', 1)
else:
key, value = line.strip().split('=', 1)
if set_environ:
os.environ[key] = value
env_vars.append({'name': key, 'value': value})
return env_vars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment