Repository navigation
Open
FunHenaXTV/python-dotenv
#14Description
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:
python-dotenv/src/dotenv/main.py
Lines 307 to 330 in 914c68e
| 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:
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
Reacted by Max Azatian
Metadata
Metadata
Assignees
Labels
No labels

Activity
solaluset commentedon Sep 1, 2022
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.
ianmackinnon commentedon Oct 13, 2022
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:Currently
python-dotenvbehaves as follows, so the backslash escape is not possible.Update tests for single-quote literal semantics (theskumar#422)
Update tests for single-quote literal semantics (theskumar#422)