python-dotenv icon indicating copy to clipboard operation
python-dotenv copied to clipboard

Multi-line variables not loading correctly with load_env

Open ldtcooper opened this issue 9 months ago • 2 comments

When reading a multi-line environment variable (e.g. an SSH key) only the first line is read when using load_env. dotenv_values appears to read the multiline variable correctly.

Tested on a Apple M2 MacBook Air running Sonoma 14.7.4 with Python 3.12.9. While I had this issue inside a functions-framework function, I was able to reproduce it outside of that environment (see below.)

As an example, try the following .env file:

FOO="foo"
BAR="bar"
BAZ1="baz
baz
baz
baz
baz
"
BAZ2="baz\nbaz\nbaz"
LAST="last"

And the following Python code:

from dotenv import load_dotenv, dotenv_values
import os

load_dotenv()
print(os.environ.get('FOO'))
print(os.environ.get('BAR'))

print(os.environ.get('BAZ1'))
print(len(os.environ.get('BAZ1')))

print(os.environ.get('BAZ2'))
print(len(os.environ.get('BAZ2')))

print(os.environ.get('LAST'))

env = dotenv_values()
print(env)

print(env.get('BAZ1'))
print(len(env.get('BAZ1')))

print(env.get('BAZ2'))
print(len(env.get('BAZ2')))

When I run that code, it produces the following output:

foo
bar
"baz
4
baz
3
last
OrderedDict({'FOO': 'foo', 'BAR': 'bar', 'BAZ1': 'baz\nbaz\nbaz\nbaz\nbaz\n', 'BAZ2': 'baz\nbaz\nbaz', 'LAST': 'last'})
baz
baz
baz
baz
baz

20
baz
baz
baz
11

ldtcooper avatar Mar 19 '25 13:03 ldtcooper

@ldtcooper Please have a look at the PR , does it solve your issue ?

Pritish053 avatar Jul 20 '25 11:07 Pritish053

Hello, I can't reproduce this result (Linux or macOS). Do you have more information about your setup?

> python --version
Python 3.12.9
> pip freeze
python-dotenv==1.1.1
> python foo.py
foo
bar
baz
baz
baz
baz
baz

20
baz
baz
baz
11
last
OrderedDict({'FOO': 'foo', 'BAR': 'bar', 'BAZ1': 'baz\nbaz\nbaz\nbaz\nbaz\n', 'BAZ2': 'baz\nbaz\nbaz', 'LAST': 'last'})
baz
baz
baz
baz
baz

20
baz
baz
baz
11

bertrand-sb avatar Jul 20 '25 14:07 bertrand-sb