Skip to content

UnicodeDecodeError exception occurs when .env file contains Non-ASCII characters on Windows #175

Closed

Description

@greyli

When .env file contains Non-ASCII characters, for example:

foo=bar
# 测试

or

foo=测试

It will cause UnicodeDecodeError: 'gbk' codec can't decode bytes in position 25-26: illegal multibyte sequence.

Tested on macOS and Windows, this problem only happened on Windows (both Python 2.7 and Python 3.6).

Here is the full error traceback:

Traceback (most recent call last):
  File "c:\python27\lib\runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "c:\python27\lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\Python27\Scripts\flask.exe\__main__.py", line 9, in <module>
  File "c:\python27\lib\site-packages\flask\cli.py", line 894, in main
    cli.main(args=args, prog_name=name)
  File "c:\python27\lib\site-packages\flask\cli.py", line 548, in main
    load_dotenv()
  File "c:\python27\lib\site-packages\flask\cli.py", line 608, in load_dotenv
    dotenv.load_dotenv(path)
  File "c:\python27\lib\site-packages\dotenv\main.py", line 309, in load_dotenv
    return DotEnv(f, verbose=verbose).set_as_environment_variables(override=override)
  File "c:\python27\lib\site-packages\dotenv\main.py", line 129, in set_as_environment_variables
    for k, v in self.dict().items():
  File "c:\python27\lib\site-packages\dotenv\main.py", line 115, in dict
    values = OrderedDict(self.parse())
  File "c:\python27\lib\collections.py", line 69, in __init__
    self.__update(*args, **kwds)
  File "c:\python27\lib\_abcoll.py", line 571, in update
    for key, value in other:
  File "c:\python27\lib\site-packages\dotenv\main.py", line 121, in parse
    for mapping in parse_stream(stream):
  File "c:\python27\lib\site-packages\dotenv\main.py", line 83, in parse_stream
    string = stream.read()
UnicodeDecodeError: 'gbk' codec can't decode bytes in position 25-26: illegal multibyte sequence

Activity

  1. changed the title [-]UnicodeDecodeError: 'gbk' codec can't decode bytes in position 25-26: illegal multibyte sequence occurs when comment contains Non-ASCII characters on Windows[/-] on Apr 22, 2019
  2. changed the title [-]UnicodeDecodeError exception occurs when comment contains Non-ASCII characters on Windows[/-] on Apr 22, 2019
  3. greyli commented on Apr 22, 2019

    @greyli
    ContributorAuthor

    This issue will be fixed with #161, closing it now.

  4. BackMountainDevil commented on Jan 25, 2021

    @BackMountainDevil

    Package Version


    Flask 1.1.2
    pip 20.3.3
    python-dotenv 0.15.0
    python 3.9.0

    windowsOS 10

    still got this problem

  5. bbc2 commented on Jan 25, 2021

    @bbc2
    Collaborator

    Do you get the exact same error? If not, could you post a detailed description of the problem, including the stack trace you get, in a new issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions