Forums

Loading my .env file from virtualenc: near unexpected token

Hi!

I've done almost everything to deploy my Django project and the only thing left is to setup the static files. But evertime i try to execute collecstatic there is some problem with the environment variables.

So used "echo 'set -a; source ~/my-project-dir/.env; set +a' >> ~/.virtualenvs/my-project-virtualenv/bin/postactivate" changing of course the correct paths.

But I think bash has a problem recognizing the .env file and it thinks that the characters in the secret key are something else. The error says:

bash: /home/switchcalendar/switchcalendar/.env: line 1: syntax error near unexpected token `here ispart of my secret key'

I hope you guys can help me :)

Is this help page what you are following?

I think it's probably because your environment variable is something like

THISENV=abc def ghi

Try making it

THISENV="abc def ghi"

Hello conrad

Yes I followed the help page.

However, i'm getting the following error message:

line 19: syntax error: unexpected end of file

Please take a look at my .env file.

My environment variables are available to the webapp, but not to the console.

Thank you.

Is line 19 the last line of the file? If so, try putting a blank line at the end; Bash can be awkward about line endings, while the python-dotenv library is more forgiving.

Yes. Line 19 is the last line of the file, and it is blank too.

Can I take a look at the file? I'd like to see its structure, but of course because it has secrets in it I can't ask you to post the contents here. However, if you give your permission, and tell me the exact path to the file, then I can take a look from our admin interface.

~/project/mysite/.env

That's the path. Permission granted. Please proceed.

Hello. I'm still waiting for a response.

OK, the problem is that two of your environment variables have exclamation marks in them; I won't list them here to avoid leaking private information, but you can easily see them. Put double quotes around the values (the bit after the = sign) and that will fix the error.

Thank you Giles.

It worked.

Great! Glad to hear it.