python-dotenv: Wrong parsing of env variables in single quotes

I have the following .env file:

DATABASE_URL='postgres://localhost:5432/myapp_development'

When I run dotenv get DATABASE_URL this is what I get: DATABASE_URL="'postgres://localhost:5432/simulator_development'"

When I try to use this with dj-database-url it is failing to parse the DATABASE_URL environment variable as it is. It seems using single quotes in the .env file is causing this.

It would be nice if this were documented somewhere if the behavior is intended I spent quite a bit of time trying to figure out where the error was. Thanks 😃

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 15 (4 by maintainers)

Most upvoted comments

@manishy635 That’s what I did, as mentioned at the end of my previous comment. My initial point was that if single quotes are used in the .env file, it doesn’t work. If this is intended, it would be nice if it was documented so that someone else doesn’t try to use single quotes in their .env file, since it wouldn’t work.

The way I see it .env files should be sourcable by shells. However, the folks at docker didn’t agree: docker-compose parses VAR= VALUE and includes the leading space in the value, as well it parses VAR='VALUE' including the quotes in the value.

This project is similar, and this is a different implicit format specification. For that reason, I did not make a PR against this project. If this project wants to change its implicit format spec to be a subset of POSIX-shell, then a larger refactor would be needed.

I wrote Envr to use a format specification that is a subset of POSIX-shell, supporting inline comments as well. But that’s a different project with a different feature set.

Is there some standard somewhere as to how .env files are meant to be parsed?

My impression was always that .env should be parsable by shells. In that case, FOOBAR='example' should definitely set the environment variable to example without the single quotes, since that is how UNIX shells work:

$ FOOBAR='example'
$ echo "$FOOBAR"
example

Guys, apologies for late reply guys.

Should be source-able

You can do this with the existing format, using this command[1]:

set -a; source .env

Single quotes not getting removed

As far as I remember I don’t a decision made to follow the docker specs (it’s got to know though). I think the issue with single quotes not getting remove got introduced accidentally while supporting the escaped characters in quoted values[2]. And should be fixed. It’s irritating as @kevgathuku mentioned.

We have been following POSIX-shell specification as close as possible, like system variable substitution, etc… The whitespaces not in quotes are trimmed as well.

I think there shouldn’t be any major compatibility issue to fix the single quote behaviour. Let me know your thoughts, I’m happy to make the change or accept a PR.

[1] https://github.com/theskumar/python-dotenv/issues/20#issuecomment-189159436 [2] https://github.com/theskumar/python-dotenv/commit/ff92e1e34c1e5b5e3f55a2a9d9fad4948c631151

@theskumar thank you! I really wondered as well because it used to work well just until a couple of days ago. This is great.