python-dotenv wrapper with attribute-like access to environment variables
This module is a simple wrapper around the well-known python-dotenv
module for Python. It provides attribute-like read access to environment variables.
INSTALLATION THROUGH pip
COMING SOON!
pip install dotenvpy
python-dotenv
already comes as a requirement for dotenvpy
; no need to worry about that.
Use it as you would use python-dotenv
:
import dotenvpy as env
env.load_dotenv()
print(env.HOME)
print(env.PATH)
# This is equivalent to:
# print(os.getenv('HOME'))
# print(os.getenv('PATH'))
dotenvpy
uses Python’s ast
module to evaluate each environment variable as a literal. This allows for automatic conversion to the right types at access time, reducing the number of explicit type conversions users have to make in order to operate with their variables.
Asume a .env
file as follows:
NON_VAR=None
BOL_VAR=True
INT_VAR=4
FLT_VAR=5.18
CPX_VAR=(2+3j)
LST_VAR=[1,2]
TPL_VAR=('1',2)
DCT_VAR={'a':1,'b':2}
STR_VAR='Just a string'
The following code would be valid with dotenvpy
:
import dotenvpy as env
env.load_dotenv()
# Without dotenvpy, this code would be quite larger
#
# At least a whole section with explicit type conversions and variable
# assignations would have been necessary
if env.NON_VAR is None and env.BOL_VAR:
a = 9 % env.INT_VAR
b = pow(env.FLT_VAR, 2)
if env.CPX_VAR.real + 1 == env.CPX_VAR.imag:
print(env.LST_VAR[1])
print(env.TPL_VAR[0])
if env.DCT_VAR['a'] + 1 == env.DCT_VAR['a']:
print(env.STR_VAR)
Since ast
is the module Python internally uses for its own interpretation, it has the same capabilities as the language literals themselves:
None
python-dotenv
.