pythonpython-dotenv

I get UnicodeDecodeError while running Flask


UnicodeDecodeError: 'utf-8' codec can't decode byte 0x99 in position 0: invalid start byte while I tried to start a Flask Server.

The following is the code

from flask import Flask
app = Flask(__name__)
app.run(debug=True, port=5000)

This generates the following error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nero/.local/lib/python3.10/site-packages/flask/app.py", line 1142, in run
    cli.load_dotenv()
  File "/home/nero/.local/lib/python3.10/site-packages/flask/cli.py", line 709, in load_dotenv
    dotenv.load_dotenv(path, encoding="utf-8")
  File "/usr/lib/python3/dist-packages/dotenv/main.py", line 332, in load_dotenv
    return dotenv.set_as_environment_variables()
  File "/usr/lib/python3/dist-packages/dotenv/main.py", line 90, in set_as_environment_variables
    for k, v in self.dict().items():
  File "/usr/lib/python3/dist-packages/dotenv/main.py", line 74, in dict
    self._dict = OrderedDict(resolve_variables(raw_values, override=self.override))
  File "/usr/lib/python3/dist-packages/dotenv/main.py", line 222, in resolve_variables
    for (name, value) in values:
  File "/usr/lib/python3/dist-packages/dotenv/main.py", line 82, in parse
    for mapping in with_warn_for_invalid_lines(parse_stream(stream)):
  File "/usr/lib/python3/dist-packages/dotenv/main.py", line 24, in with_warn_for_invalid_lines
    for mapping in mappings:
  File "/usr/lib/python3/dist-packages/dotenv/parser.py", line 180, in parse_stream
    reader = Reader(stream)
  File "/usr/lib/python3/dist-packages/dotenv/parser.py", line 71, in __init__
    self.string = stream.read()
  File "/usr/lib/python3.10/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x99 in position 0: invalid start byte

This is a bare code and it should not generate any errors but it does,

Attaching a screenshot for reference

enter image description here

My Environment settings are

Python 3.10.6

Ubuntu 22.04 - Linux [Tested on a Windows machine also]

Flask 2.2.2

Thanks in Advance

NB :

I tried to run a flask server with default configurations. Expecting to run Flask Server


Solution

  • If you are here with the same issue Here is the solution

    I assumed it had nothing to do with the code I wrote, as it is barely anything to make errors.

    Reading through the Error traceback, I saw it is related to dotenv which in turn is a python package that deals with environment variables and secrets.

    Creating a bare .env file in root solved the problem