From 7a9232ff4ef5a61b379e457eab1a14d1a528261a Mon Sep 17 00:00:00 2001 From: Isaac Slavitt Date: Sun, 1 May 2016 14:14:56 -0400 Subject: [PATCH] Update docs and src for new python-dotenv release --- docs/docs/index.md | 8 ++++++-- {{ cookiecutter.repo_name }}/src/data/make_dataset.py | 9 ++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/docs/docs/index.md b/docs/docs/index.md index 65416e6..984e375 100644 --- a/docs/docs/index.md +++ b/docs/docs/index.md @@ -203,10 +203,14 @@ If you look at the stub script in `src/data/make_dataset.py`, it uses a package ```python # src/data/dotenv_example.py from os.path import join, dirname -from dotenv import load_dotenv +from dotenv import load_dotenv, find_dotenv -dotenv_path = join(dirname(__file__), os.pardir, os.pardir, '.env') # up two levels to root folder +# find .env automagically by walking up directories until it's found +dotenv_path = find_dotenv() + +# load up the entries as environment variables load_dotenv(dotenv_path) + database_url = os.environ.get("DATABASE_URL") other_variable = os.environ.get("OTHER_VARIABLE") ``` diff --git a/{{ cookiecutter.repo_name }}/src/data/make_dataset.py b/{{ cookiecutter.repo_name }}/src/data/make_dataset.py index 66e3192..bc50a72 100644 --- a/{{ cookiecutter.repo_name }}/src/data/make_dataset.py +++ b/{{ cookiecutter.repo_name }}/src/data/make_dataset.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- import os import click -import dotenv import logging +from dotenv import find_dotenv, load_dotenv @click.command() @@ -17,9 +17,12 @@ if __name__ == '__main__': log_fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' logging.basicConfig(level=logging.INFO, format=log_fmt) + # not used in this stub but often useful for finding various files project_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir) - dotenv_path = os.path.join(project_dir, '.env') - dotenv.load_dotenv(dotenv_path) + + # find .env automagically by walking up directories until it's found, then + # load up the .env entries as environment variables + load_dotenv(find_dotenv()) main()