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

no error or failure is reported when specified .env file does not exist #199

Closed
rptaylor opened this issue Dec 24, 2020 · 2 comments
Closed

Comments

@rptaylor
Copy link

rptaylor commented Dec 24, 2020

Hello,

This package is very useful, thanks!
I noticed something while testing opening a .env file that does not exist:

$ python
Python 3.9.1 (default, Dec 18 2020, 05:16:04) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from environs import Env
>>> env = Env()
>>> env.read_env("nothing.env", verbose=True)
>>> env("VAR")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/home/.local/lib/python3.9/site-packages/environs/__init__.py", line 80, in method
    raise EnvError('Environment variable "{}" not set'.format(proxied_key or parsed_key))
environs.EnvError: Environment variable "VAR" not set

There is no error message or warning when calling read_env on a file that does not exist - even with verbose=True !
Instead, the environment variables you may have expected will just be absent. Is this intended?
This could cause your application to perhaps silently behave differently than expected.
IMO if there is a problem the failure should be immediate and obvious instead of deferred and quiet.
So if you explicitly specify the env file that you want, there should be an exception if it doesn't exist.
This way it can be detected earlier, before the application starts running and getting environs.EnvError from missing variables. Moreover this clearly distinguishes between two different problems: some lines missing from the .env file, or the whole .env file missing altogether.

So instead it seems I will have to write some error handling to detect if the env file exists before calling read_env. Not sure about the design reasoning but thought it might be helpful if environs could handle that instead.

I note that upstream dotenv has this line but not sure why it isn't being invoked:
https://github.com/theskumar/python-dotenv/blob/master/src/dotenv/main.py#L65

$ pip show environs
Name: environs
Version: 9.2.0
Summary: simplified environment variable parsing
Home-page: https://github.com/sloria/environs
Author: Steven Loria
Author-email: [email protected]
License: MIT
Location: /tmp/home/.local/lib/python3.9/site-packages
Requires: python-dotenv, marshmallow
Required-by: 
@rptaylor
Copy link
Author

https://github.com/sloria/environs/blob/master/environs/__init__.py#L341
Counterintuitively, you get an error if you specify an existing directory, but no error with a non-existent directory.

@sloria
Copy link
Owner

sloria commented Dec 26, 2020

This was proposed in #165 . The decision was to follow python-dotenv's lead. If python-dotenv adds API for this, we can expose it in environs, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants