Skip to content

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

@BackMountainDevil

Description

@BackMountainDevil

Similar to Issue #175 UnicodeDecodeError exception occurs when .env file contains Non-ASCII characters on Windows.
And that issue said it sloved in PR 161 30 Apr 2019, but it did happen in the version 0.15.0 which released on 29 Oct 2020.
Similar situation I met before about decode on windows

Info about env

OS like

  • windows 10 amd64
  • python 3.9.0

pip package info

Package       Version
------------- -------
click         7.1.2
Flask         1.1.2
itsdangerous  1.1.0
Jinja2        2.11.2
MarkupSafe    1.1.1
pip           20.3.3
python-dotenv 0.15.0
setuptools    51.3.3
watchdog      1.0.2
Werkzeug      1.0.1
wheel         0.36.2

what situation will error

code

see rpo

point

When I add a little Chinese comment in file - .flaskenv, it will happen. If delete all Chinese comment, it work fine.

bad result

add a comment to the end of .flaskenv. Just like this

# -*- encoding: utf-8 -*-
# Public env Variables about flask

# FLASK_APP = app.py
# FLASK_RUN_HOST = 127.0.0.1
FLASK_RUN_PORT = 80

# dev mode 
FLASK_ENV = development
# 今天也是个小可爱耶

Then run flask

PS D:\Documents\CAU\Lion\repositiries\Python\fb> flask run
Traceback (most recent call last):
  File "d:\program files\python\python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "d:\program files\python\python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\users\kearney\.virtualenvs\fb-xzc3iotr\lib\site-packages\flask\cli.py", line 967, in main
    cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None)
  File "c:\users\kearney\.virtualenvs\fb-xzc3iotr\lib\site-packages\flask\cli.py", line 575, in main
    load_dotenv()
  File "c:\users\kearney\.virtualenvs\fb-xzc3iotr\lib\site-packages\flask\cli.py", line 649, in load_dotenv
    dotenv.load_dotenv(path)
  File "c:\users\kearney\.virtualenvs\fb-xzc3iotr\lib\site-packages\dotenv\main.py", line 319, in load_dotenv
    return DotEnv(f, verbose=verbose, interpolate=interpolate, **kwargs).set_as_environment_variables(override=override)
  File "c:\users\kearney\.virtualenvs\fb-xzc3iotr\lib\site-packages\dotenv\main.py", line 106, in set_as_environment_variables
    for k, v in self.dict().items():
  File "c:\users\kearney\.virtualenvs\fb-xzc3iotr\lib\site-packages\dotenv\main.py", line 87, in dict
    values = resolve_nested_variables(self.parse())
  File "c:\users\kearney\.virtualenvs\fb-xzc3iotr\lib\site-packages\dotenv\main.py", line 239, in resolve_nested_variables
    for (k, v) in values:
  File "c:\users\kearney\.virtualenvs\fb-xzc3iotr\lib\site-packages\dotenv\main.py", line 97, in parse
    for mapping in with_warn_for_invalid_lines(parse_stream(stream)):
  File "c:\users\kearney\.virtualenvs\fb-xzc3iotr\lib\site-packages\dotenv\main.py", line 48, in with_warn_for_invalid_lines
    for mapping in mappings:
  File "c:\users\kearney\.virtualenvs\fb-xzc3iotr\lib\site-packages\dotenv\parser.py", line 229, in parse_stream
    reader = Reader(stream)
  File "c:\users\kearney\.virtualenvs\fb-xzc3iotr\lib\site-packages\dotenv\parser.py", line 107, in __init__
    self.string = stream.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xaa in position 194: illegal multibyte sequence

good reslut

PS D:\Documents\CAU\Lion\repositiries\Python\fb> flask run
 * Environment: development
 * Debug mode: on
 * Restarting with windowsapi reloader
 * Debugger is active!
 * Debugger PIN: 139-670-554
 * Running on http://127.0.0.1:80/ (Press CTRL+C to quit)

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