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).
Its simple. env always stores string in not only just python but also in javascript. simply parse the env value with json import json DEBUG = json.loads(os.getenv("DEBUG"))
if DEBUG:
print("Debugging")
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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
Its simple. env always stores string in not only just python but also in javascript. simply parse the env value with json
import json
DEBUG = json.loads(os.getenv("DEBUG"))
if DEBUG:
print("Debugging")
Okay I'm surprised I didn't know that! Thanks. This'll save me some future headaches.
Sure! After time spent to True this, False that... and your app lives of it’s own life
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.