I have the following environment variables in my .env
file:
DT="2019-01-01"
X=${DT//-/}
The variable X
has been set using Bash's parameter replacement, using the ${parameter//pattern/string}
format to replace all occurrences (Documentation here).
Now, to read the environment variables into Python, I have created a Python class Config
in a file config.py
:
from dotenv import find_dotenv, load_dotenv
import os
class Config:
def __init__(self):
load_dotenv(find_dotenv())
self.X = os.environ.get('X')
In a python
shell, I run:
In [1]: from config import Config
In [2]: c = Config()
In [3]: c.X
Out[3]: ''
Here c.X
is an empty string ''
, where as I want it to be '20190101'
.
How do I load the correct value of the environment variable into a python variable?
Edit: When I type echo $X
in a bash script, it prints the correct value. For example, a bash script sample.sh
:
#!/bin/bash
source .env
echo $X
When run, I get:
$ sh sample.sh
20190101
I added an export
in my .env
file in this way:
DT="2019-01-01"
export X=${DT//-/}
This allowed me to get the correct value of c.X
in Python.