Last active
August 17, 2018 23:54
-
-
Save radzhome/9f33937caa26bbe0aee65bd394d8ce1c to your computer and use it in GitHub Desktop.
python read env file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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