Skip to content

Instantly share code, notes, and snippets.

@radzhome
Last active August 17, 2018 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radzhome/9f33937caa26bbe0aee65bd394d8ce1c to your computer and use it in GitHub Desktop.
Save radzhome/9f33937caa26bbe0aee65bd394d8ce1c to your computer and use it in GitHub Desktop.
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