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

Handle error accessing .env file #3

Closed
johnjohndoe opened this issue Jan 27, 2015 · 6 comments
Closed

Handle error accessing .env file #3

johnjohndoe opened this issue Jan 27, 2015 · 6 comments

Comments

@johnjohndoe
Copy link

I stumbled into an error caused by a typo in wsgi.py which prevented the settings from .env being imported. Since errors are not handled it took me days to find the actual problem. Therefore, I suggest you introduce some kind of error handling to notify the user.

I introduced an AttributeError as shown below. You might wanna replace it with a more general error class.

try:
    dotenv.read_dotenv(
        os.path.join(os.path.dirname(os.path.dirname(__file__)), '.env'))
except AttributeError as e:
    print "Error reading .env: {0}".format(e)
    pass
@jpadilla
Copy link
Owner

Hmm, that's definitely not a typo. It's meant to catch any exception. When a file isn't found django-dotenv will issue a warning(not an exception) like "Not reading .env - it doesn't exist." The reason it's just a warning and not an exception is because the .env file is completely optional, mostly meant for development, while using real environment variables whenever you deploy to production, usually using the wsgi.py + something like gunicorn. I added the option to read the .env from the wsgi file as well for convenience while testing that out in development.

@johnjohndoe
Copy link
Author

I had a typo here ("direname"):

os.path.join(os.path.dirname(os.path.direname(file)), '.env'))

This caused the failure to read the .env file.

Do I understand you right? You do not use .env in production?

@jpadilla
Copy link
Owner

Ah gotcha, yeah instead of swallowing any exception I should just be printing out whatever the message for the exception was, that would be good.

Yeah, I don't even commit my .env into my repository, it should be ignored. Though, I usually deploy on Heroku. But there's nothing stopping you of actually deploying your .env to your server and using that, don't see anything wrong with that.

@johnjohndoe
Copy link
Author

Printing out whatever message comes would be helpful.

I normally commit an example.env which has all the property names. The .env prevented to be commited via .gitignore.

@jpadilla
Copy link
Owner

Done here 3b7a398

@johnjohndoe
Copy link
Author

Cool.

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