Make sure your .env file only contains data in the following format:
MY_ENV_VAR = value
Anything other than this and you will get NoneType
if you are trying to retrieve them.
When you are trying to retrieve these you can do the following:
from pathlib import Path
from dotenv import load_dotenv
env_path = Path('.', '.env')
load_dotenv(dotenv_path=env_path)
my_env_var = os.getenv('MY_ENV_VAR')
The env_path
is simply the path to your .env
file. The '.' is the root directory of your app. You can even pass it in the dotenv_path
argument like '\path\to\your\.env'
e.g. load_dotenv(dotenv_path='\path\to\your\.env')
.
EDIT:
If you are adding it in your terminal, make sure there is no whitespace around the =
sign. For instance:
Linux:
$ export MY_ENV_VAR=value
Windows:
> set MY_ENV_VAR=value