Skip to content

Commit c50c6f7

Browse files
earlbreadbbc2
authored andcommitted
Add argument to choose .env file encoding, defaults to None
Added argument for .env file encoding. Default encoding is None.
1 parent 2767b35 commit c50c6f7

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ DATABASE_PASSWORD = os.getenv("DATABASE_PASSWORD")
103103
`load_dotenv` do not override existing System environment variables. To
104104
override, pass `override=True` to `load_dotenv()`.
105105

106+
`load_dotenv` also accepts `encoding` parameter to open the `.env` file. The default encoding is platform dependent (whatever `locale.getpreferredencoding()` returns), but any encoding supported by Python can be used. See the [codecs](https://docs.python.org/3/library/codecs.html#standard-encodings) module for the list of supported encodings.
107+
106108
You can use `find_dotenv()` method that will try to find a `.env` file
107109
by (a) guessing where to start using `__file__` or the working directory
108110
-- allowing this to work in non-file contexts such as IPython notebooks
@@ -298,6 +300,7 @@ Unreleased
298300
-----
299301

300302
- Add type hints and expose them to users ([@qnighy])([#172])
303+
- `load_dotenv` and `dotenv_values` now accepts `encoding` paramater, defaults to `None` ([@theskumar])([@earlbread]) (#161)
301304

302305
0.10.1
303306
-----
@@ -409,6 +412,7 @@ Unreleased
409412
[@hugochinchilla](https://github.com/hugochinchilla)).
410413
- Improved test coverage.
411414

415+
[#161]: https://github.com/theskumar/python-dotenv/issues/161
412416
[#78]: https://github.com/theskumar/python-dotenv/issues/78
413417
[#148]: https://github.com/theskumar/python-dotenv/issues/148
414418
[#158]: https://github.com/theskumar/python-dotenv/issues/158
@@ -423,3 +427,4 @@ Unreleased
423427
[@cjauvin]: https://github.com/cjauvin
424428
[@bbc2]: https://github.com/bbc2
425429
[@qnighy]: https://github.com/qnighy
430+
[@earlbread]: https://github.com/earlbread

src/dotenv/main.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,20 @@ def parse_stream(stream):
111111

112112
class DotEnv():
113113

114-
def __init__(self, dotenv_path, verbose=False):
115-
# type: (Union[Text, _PathLike, _StringIO], bool) -> None
114+
def __init__(self, dotenv_path, verbose=False, encoding=None):
115+
# type: (Union[Text, _PathLike, _StringIO], bool, Union[None, Text]) -> None
116116
self.dotenv_path = dotenv_path # type: Union[Text,_PathLike, _StringIO]
117117
self._dict = None # type: Optional[Dict[Text, Text]]
118118
self.verbose = verbose # type: bool
119+
self.encoding = encoding # type: Union[None, Text]
119120

120121
@contextmanager
121122
def _get_stream(self):
122123
# type: () -> Iterator[IO[Text]]
123124
if isinstance(self.dotenv_path, StringIO):
124125
yield self.dotenv_path
125126
elif os.path.isfile(self.dotenv_path):
126-
with io.open(self.dotenv_path) as stream:
127+
with io.open(self.dotenv_path, encoding=self.encoding) as stream:
127128
yield stream
128129
else:
129130
if self.verbose:
@@ -349,16 +350,16 @@ def find_dotenv(filename='.env', raise_error_if_not_found=False, usecwd=False):
349350
return ''
350351

351352

352-
def load_dotenv(dotenv_path=None, stream=None, verbose=False, override=False):
353-
# type: (Union[Text, _PathLike, None], Optional[_StringIO], bool, bool) -> bool
353+
def load_dotenv(dotenv_path=None, stream=None, verbose=False, override=False, **kwargs):
354+
# type: (Union[Text, _PathLike, None], Optional[_StringIO], bool, bool, Union[None, Text]) -> bool
354355
f = dotenv_path or stream or find_dotenv()
355-
return DotEnv(f, verbose=verbose).set_as_environment_variables(override=override)
356+
return DotEnv(f, verbose=verbose, **kwargs).set_as_environment_variables(override=override)
356357

357358

358-
def dotenv_values(dotenv_path=None, stream=None, verbose=False):
359-
# type: (Union[Text, _PathLike, None], Optional[_StringIO], bool) -> Dict[Text, Text]
359+
def dotenv_values(dotenv_path=None, stream=None, verbose=False, **kwargs):
360+
# type: (Union[Text, _PathLike, None], Optional[_StringIO], bool, Union[None, Text]) -> Dict[Text, Text]
360361
f = dotenv_path or stream or find_dotenv()
361-
return DotEnv(f, verbose=verbose).dict()
362+
return DotEnv(f, verbose=verbose, **kwargs).dict()
362363

363364

364365
def run_command(command, env):

0 commit comments

Comments
 (0)