diff --git a/dotenv/main.py b/dotenv/main.py index cdb5d9d4..252a2dd3 100644 --- a/dotenv/main.py +++ b/dotenv/main.py @@ -232,7 +232,11 @@ def find_dotenv(filename='.env', raise_error_if_not_found=False, usecwd=False): path = os.getcwd() else: # will work for .py files - frame_filename = sys._getframe().f_back.f_code.co_filename + frame = sys._getframe() + # find first frame that is outside of this file + while frame.f_code.co_filename == __file__: + frame = frame.f_back + frame_filename = frame.f_code.co_filename path = os.path.dirname(os.path.abspath(frame_filename)) for dirname in _walk_to_root(path): diff --git a/tests/test_core.py b/tests/test_core.py index a7fb347c..1547e245 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1,4 +1,4 @@ -# -*- coding: utf8 -*- +# -*- coding: utf-8 -*- from __future__ import unicode_literals import os @@ -107,6 +107,19 @@ def test_load_dotenv_override(cli): sh.rm(dotenv_path) +def test_load_dotenv_in_current_dir(): + # make sure were are here! + os.chdir(os.path.dirname(os.path.realpath(__file__))) + dotenv_path = '.env' + with open(dotenv_path, 'w') as f: + f.write("TOTO=bla\n") + assert 'TOTO' not in os.environ + success = load_dotenv(verbose=True) + assert success + assert os.environ['TOTO'] == 'bla' + sh.rm(dotenv_path) + + def test_ipython(): tmpdir = os.path.realpath(tempfile.mkdtemp()) os.chdir(tmpdir)