Python-dotenv could not parse statement starting at line 2

Sivapriya Sridhar picture Sivapriya Sridhar · Jun 17, 2020 · Viewed 7.3k times · Source

Python-dotenv could not parse statement starting at line 2

I have uninstalled and reinstalled python-dotenv still i get same error. Could anyone sort this?

Answer

TheButcherOfBlaviken picture TheButcherOfBlaviken · Nov 17, 2020

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