Issue
How can I evaluate if a env variable is a boolean True, in Python? Is it correct to use:
if os.environ['ENV_VAR'] is True:
.......
Solution
I think this works well:
my_env = os.getenv("ENV_VAR", 'False').lower() in ('true', '1', 't')
It allows: things like true
, True
, TRUE
, 1
, "1"
, TrUe
, t
, T
, ...
Update: After I read the commentary of Klaas, I updated the original code my_env = bool(os.getenv(...
to my_env = os.getenv(...
because in
will result in a bool
type
Answered By - Rui Martins
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.