Skip to content

Commit b158aa7

Browse files
committed
Use UTF-8 as default encoding
The default value for the `encoding` paramter of `load_dotenv` and `dotenv_values` is now `"utf-8"` instead of `None` (which selected the encoding based on the user's locale). It is passed directly to `io.open`. The rationale for this change is that the encoding of a project file like `.env` should not depend on the user's locale by default. UTF-8 makes sense as the default encoding since it is also used for Python source files. The main drawback is that it departs from `open`'s default value of `None` for the `encoding` parameter. The default value of `None` was a source of confusion for some users. The Flask and Docker Compose projects already use `encoding="utf-8"` to enforce the use of UTF-8 and avoid that sort of confusion. This is a breaking change but only for users with a non-UTF-8 locale and non-UTF-8 characters in their .env files.
1 parent a7fe93f commit b158aa7

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
99

10-
- Fix resolution order in variable expansion with `override=False` (#? by [@bbc2]).
10+
### Changed
11+
12+
- The default value of the `encoding` parameter for `load_dotenv` and `dotenv_values` is
13+
now `"utf-8"` instead of `None` (#? by [@bbc2]).
14+
- Fix resolution order in variable expansion with `override=False` (#287 by [@bbc2]).
1115

1216
## [0.15.0] - 2020-10-28
1317

src/dotenv/main.py

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,22 +293,63 @@ def _is_interactive():
293293
return ''
294294

295295

296-
def load_dotenv(dotenv_path=None, stream=None, verbose=False, override=False, interpolate=True, **kwargs):
297-
# type: (Union[Text, _PathLike, None], Optional[_StringIO], bool, bool, bool, Union[None, Text]) -> bool
296+
def load_dotenv(
297+
dotenv_path=None,
298+
stream=None,
299+
verbose=False,
300+
override=False,
301+
interpolate=True,
302+
encoding="utf-8",
303+
):
304+
# type: (Union[Text, _PathLike, None], Optional[_StringIO], bool, bool, bool, Optional[Text]) -> bool # noqa
298305
"""Parse a .env file and then load all the variables found as environment variables.
299306
300307
- *dotenv_path*: absolute or relative path to .env file.
301-
- *stream*: `StringIO` object with .env content.
302-
- *verbose*: whether to output the warnings related to missing .env file etc. Defaults to `False`.
303-
- *override*: where to override the system environment variables with the variables in `.env` file.
304-
Defaults to `False`.
308+
- *stream*: `StringIO` object with .env content, used if `dotenv_path` is `None`.
309+
- *verbose*: whether to output a warning the .env file is missing. Defaults to
310+
`False`.
311+
- *override*: whether to override the system environment variables with the variables
312+
in `.env` file. Defaults to `False`.
313+
- *encoding*: encoding to be used to read the file.
314+
315+
If both `dotenv_path` and `stream`, `find_dotenv()` is used to find the .env file.
305316
"""
306317
f = dotenv_path or stream or find_dotenv()
307-
dotenv = DotEnv(f, verbose=verbose, interpolate=interpolate, override=override, **kwargs)
318+
dotenv = DotEnv(
319+
f,
320+
verbose=verbose,
321+
interpolate=interpolate,
322+
override=override,
323+
encoding=encoding,
324+
)
308325
return dotenv.set_as_environment_variables()
309326

310327

311-
def dotenv_values(dotenv_path=None, stream=None, verbose=False, interpolate=True, **kwargs):
312-
# type: (Union[Text, _PathLike, None], Optional[_StringIO], bool, bool, Union[None, Text]) -> Dict[Text, Optional[Text]] # noqa: E501
328+
def dotenv_values(
329+
dotenv_path=None,
330+
stream=None,
331+
verbose=False,
332+
interpolate=True,
333+
encoding="utf-8",
334+
):
335+
# type: (Union[Text, _PathLike, None], Optional[_StringIO], bool, bool, Optional[Text]) -> Dict[Text, Optional[Text]] # noqa: E501
336+
"""
337+
Parse a .env file and return its content as a dict.
338+
339+
- *dotenv_path*: absolute or relative path to .env file.
340+
- *stream*: `StringIO` object with .env content, used if `dotenv_path` is `None`.
341+
- *verbose*: whether to output a warning the .env file is missing. Defaults to
342+
`False`.
343+
in `.env` file. Defaults to `False`.
344+
- *encoding*: encoding to be used to read the file.
345+
346+
If both `dotenv_path` and `stream`, `find_dotenv()` is used to find the .env file.
347+
"""
313348
f = dotenv_path or stream or find_dotenv()
314-
return DotEnv(f, verbose=verbose, interpolate=interpolate, override=True, **kwargs).dict()
349+
return DotEnv(
350+
f,
351+
verbose=verbose,
352+
interpolate=interpolate,
353+
override=True,
354+
encoding=encoding,
355+
).dict()

0 commit comments

Comments
 (0)