Python-dotenv could not parse statement starting at line 2

17,175

Solution 1

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 

Solution 2

I'm seeing this too. It happens if the last line in the .env file is empty.

Some quick testing shows that it appeared in 0.10.4; with 0.10.3 no warning is displayed.

https://github.com/theskumar/python-dotenv/issues/235

This may helps

Solution 3

For me the problem disappeared when I deleted space after equality sign and removed apostrophes (') and quotation marks (") from my .env file. So instead of this .env:

FOO = 'something'
BAR = "something_else"

Try changing .env to:

FOO=something
BAR=something_else
Share:
17,175
Admin
Author by

Admin

Updated on July 29, 2022

Comments

  • Admin
    Admin 10 months

    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?