Skip to content

Unable to escape posix expansion #422

Open
FunHenaXTV/python-dotenv
#14

Description

@scottzach1

What

Unable to elegantly escape the $ in ${VARIABLE} expansion.

Scenario

.env

VAR=${VARIABLE}

script.py

import os
import dotenv

if dotenv.find_dotenv():
    dotenv.load_dotenv(interpolate=False)

print(f"VAR={os.getenv('VAR')}"

I do not see the interpolate kwarg in the documentation, however looking at the stub:

def load_dotenv(
dotenv_path: Union[str, os.PathLike, None] = None,
stream: Optional[IO[str]] = None,
verbose: bool = False,
override: bool = False,
interpolate: bool = True,
encoding: Optional[str] = "utf-8",
) -> bool:
"""Parse a .env file and then load all the variables found as environment variables.
Parameters:
dotenv_path: Absolute or relative path to .env file.
stream: Text stream (such as `io.StringIO`) with .env content, used if
`dotenv_path` is `None`.
verbose: Whether to output a warning the .env file is missing.
override: Whether to override the system environment variables with the variables
from the `.env` file.
encoding: Encoding to be used to read the file.
Returns:
Bool: True if atleast one environment variable is set elese False
If both `dotenv_path` and `stream` are `None`, `find_dotenv()` is used to find the
.env file.
"""

*Edit: and the pypi documentation:

image

Expected Behavior

output

>> VAR='${VARIABLE}'

Actual Behavior

output

>> VAR=''

Workaround

My current workaround is to provide the desired string as a default value for variable expansion.

.env

VAR=${_:-${VARIABLE}]

output

>> VAR='${VARIABLE}'

System

Host: Windows 11
Python: 3.10 w/ Poetry environment

Activity

  1. solaluset commented on Sep 1, 2022

    @solaluset

    I upvote this. The logical way to do this would be using single quotes (there's no interpolation of single-quoted strings in bash), but the library treats it same as double quotes.

  2. ianmackinnon commented on Oct 13, 2022

    @ianmackinnon

    Adding the option to escape the dollar symbol with a backslash would match the behaviour of Bash (and the Node module dotenv-expand). For example, in Bash:

    KEY=value
    K1=\$KEY
    K2=\${KEY}
    K3="\$KEY"
    K4="\${KEY}"
    echo $K1, $K2, $K3, $K4
    
    # $KEY, ${KEY}, $KEY, ${KEY}

    Currently python-dotenv behaves as follows, so the backslash escape is not possible.

    from io import StringIO
    from dotenv import dotenv_values
    
    dotenv_values(stream=StringIO(r"""
    KEY=value
    K1=\$KEY
    K2=\${KEY}
    K3="\$KEY"
    K4="\${KEY}"
    """))
    
    # OrderedDict([('KEY', 'value'), ('K1', '\\$KEY'), ('K2', '\\value'), ('K3', '\\$KEY'), ('K4', '\\value')])
  3. self-assigned this
    on Apr 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions