Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python-dotenv could not parse statement starting at line 2

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?

like image 785
Sivapriya Sridhar Avatar asked Jun 17 '20 03:06

Sivapriya Sridhar


People also ask

Should Python-dotenv output a warning when running a python script?

Some quick testing shows that it appeared in 0.10.4; with 0.10.3 no warning is displayed. Sorry, something went wrong. This is indeed surprising. It looks like your .env is fine so Python-dotenv shouldn't output a warning.

How do I parse an ENV file in Docker-Compose?

docker-compose uses python-dotenv for parsing env files. Keys can be unquoted, and single-quoted. Values can be unquoted, single- and double-quoted. Spaces before and after keys, equal signs, and values are ignored. Values can be followed by a comment.

What is dotenv in Docker-Compose?

docker-compose uses python-dotenv for parsing env files. Keys can be unquoted, and single-quoted. Values can be unquoted, single- and double-quoted. Spaces before and after keys, equal signs, and values are ignored.


Video Answer


3 Answers

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 
like image 64
nabil.adnan1610 Avatar answered Oct 07 '22 02:10

nabil.adnan1610


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
like image 39
P D Avatar answered Oct 07 '22 02:10

P D


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

like image 30
jsp Avatar answered Oct 07 '22 03:10

jsp