Yeah, that's an unfortunate drawback of dotenv. There's a couple of things you can do.
Explicitly check for a string value. DEBUG = (os.getenv("DEBUG") == 'true')
Cast the val to a boolean DEBUG = bool(os.getenv("DEBUG")), and use an empty string to denote a false value DEBUG=''.
Use a more fully-featured package like django-environ. There's slightly more configuration required, but if your project has multiple boolean settings it might be worth it. (I haven't actually used django-environ, but it looks pretty interesting so I may investigate).
This is really helpful, thanks. Agreed—it seems strange that your PR was rejected, given that variables with False values get (confusingly) cast to True.
Thanks James! I'm starting using this :) I'm moving from PyCharm to VSCode, and I notice use .env files in VSCode is more easy using "envFile" parameter in launch.json without any plugin and pip pacakge extra.
I used to do this. Now I just have a mysecrets.py file (which is not in the repo obviously) and just type "from mysecrets import *" at the top of settings.py. The problem I had with .env is with debug mode. Say someone on your team accidentally deploys in prod with DEBUG=True. If an end-user hits a python exception for some reason, the environment variables all appear on the Django debug screen. In contrast, regular python variables in settings.py are obfuscated by Django. Have you noticed this? Does it concern you?
How do you manage Boolean with python-dotenv? I mean that ( for example)DEBUG = True or DEBUG = False in .env file are always evaluated as True
Yeah, that's an unfortunate drawback of dotenv. There's a couple of things you can do.
DEBUG=''.thanks for this
Don't ask my why the author did not accept my PR: github.com/theskumar/python-dotenv...
Converting types
The library reads and provides strings. If you need for example a boolean, it is up to you to convert the value.
Example:
I agree, it works perfectly
This is really helpful, thanks. Agreed—it seems strange that your PR was rejected, given that variables with False values get (confusingly) cast to True.
Thanks James! I'm starting using this :) I'm moving from PyCharm to VSCode, and I notice use
.envfiles in VSCode is more easy using"envFile"parameter inlaunch.jsonwithout any plugin and pip pacakge extra.I used to do this. Now I just have a mysecrets.py file (which is not in the repo obviously) and just type "from mysecrets import *" at the top of settings.py. The problem I had with .env is with debug mode. Say someone on your team accidentally deploys in prod with DEBUG=True. If an end-user hits a python exception for some reason, the environment variables all appear on the Django debug screen. In contrast, regular python variables in settings.py are obfuscated by Django. Have you noticed this? Does it concern you?
This produces errors when you push your app to heroku for hosting
Were you able to fix this?
Awesome, thanks!