Skip to content

Commit 5238808

Browse files
cjauvintheskumar
authored andcommitted
Fix: enable loading from current directory (theskumar#109)
* Fix to enable loading from current directory * Add test to make sure loading from current dir works * Reorder test_core tests to avoid directory problem * Reorder curr dir test again, and use os.chdir
1 parent b20d818 commit 5238808

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

dotenv/main.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,11 @@ def find_dotenv(filename='.env', raise_error_if_not_found=False, usecwd=False):
232232
path = os.getcwd()
233233
else:
234234
# will work for .py files
235-
frame_filename = sys._getframe().f_back.f_code.co_filename
235+
frame = sys._getframe()
236+
# find first frame that is outside of this file
237+
while frame.f_code.co_filename == __file__:
238+
frame = frame.f_back
239+
frame_filename = frame.f_code.co_filename
236240
path = os.path.dirname(os.path.abspath(frame_filename))
237241

238242
for dirname in _walk_to_root(path):

tests/test_core.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# -*- coding: utf8 -*-
1+
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals
33

44
import os
@@ -107,6 +107,19 @@ def test_load_dotenv_override(cli):
107107
sh.rm(dotenv_path)
108108

109109

110+
def test_load_dotenv_in_current_dir():
111+
# make sure were are here!
112+
os.chdir(os.path.dirname(os.path.realpath(__file__)))
113+
dotenv_path = '.env'
114+
with open(dotenv_path, 'w') as f:
115+
f.write("TOTO=bla\n")
116+
assert 'TOTO' not in os.environ
117+
success = load_dotenv(verbose=True)
118+
assert success
119+
assert os.environ['TOTO'] == 'bla'
120+
sh.rm(dotenv_path)
121+
122+
110123
def test_ipython():
111124
tmpdir = os.path.realpath(tempfile.mkdtemp())
112125
os.chdir(tmpdir)

0 commit comments

Comments
 (0)