Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable ".env not found" warning by default #57

Merged
merged 3 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ Add the following code to your ``settings.py``
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)

# OR, the same with increased verbosity:
load_dotenv(dotenv_path, verbose=True)

Alternatively, you can use ``find_dotenv()`` method that will try to find a
``.env`` file by (a) guessing where to start using ``__file__`` or the working
directory -- allowing this to work in non-file contexts such as IPython notebooks
Expand Down
5 changes: 3 additions & 2 deletions dotenv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ def decode_escaped(escaped):
return __escape_decoder(escaped)[0]


def load_dotenv(dotenv_path):
def load_dotenv(dotenv_path, verbose=False):
"""
Read a .env file and load into os.environ.
"""
if not os.path.exists(dotenv_path):
warnings.warn("Not loading %s - it doesn't exist." % dotenv_path)
if verbose:
warnings.warn("Not loading %s - it doesn't exist." % dotenv_path)
return None
for k, v in dotenv_values(dotenv_path).items():
os.environ.setdefault(k, v)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

def test_warns_if_file_does_not_exist():
with warnings.catch_warnings(record=True) as w:
load_dotenv('.does_not_exist')
load_dotenv('.does_not_exist', verbose=True)

assert len(w) == 1
assert w[0].category is UserWarning
Expand Down