1

After cloning a personal Django project from Github on to my computer, some environment variables were written to a .env file. The variables encompass a Django generated SECRET_KEY surrounded my single quotes and setting DEBUG to a string of 'False'. I installed python-dotenv as a part of my requirements for the purpose of passing in those variables into settings.py. Afterwards, I ran python manage.py migrate, yet I get the following error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte.

I've researched the matter here:

As of python-dotenv 1.0.0 (the version which is installed), load_dotenv() has a default parameter of encoding=utf-8

I'm doing all of this through Powershell on Windows 10 and using Python version 3.9.6. What else should I try to resolve this error?

Traceback (most recent call last):
  File "C:\..\django_stackoverflow\manage.py", line 22, in <module>
    main()
  File "C:\..\django_stackoverflow\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\..\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\..\django\core\management\__init__.py", line 363, in execute
    settings.INSTALLED_APPS
  File "C:\..\site-packages\django\conf\__init__.py", line 82, in __getattr__
    self._setup(name)
  File "C:\..\django\conf\__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\..\django\conf\__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\..\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  
  File "C:\..\stackoverflow_clone\settings.py", line 17, in <module>
    load_dotenv(encoding='utf8')
  File "C:\..\site-packages\dotenv\main.py", line 346, in load_dotenv
    return dotenv.set_as_environment_variables()
  File "C:\..\site-packages\dotenv\main.py", line 91, in set_as_environment_variables
    if not self.dict():
  File "C:\..b\site-packages\dotenv\main.py", line 75, in dict
    self._dict = OrderedDict(resolve_variables(raw_values, override=self.override))
  File "C:\..\site-packages\dotenv\main.py", line 233, in resolve_variables
    for (name, value) in values:
  File "C:\..\site-packages\dotenv\main.py", line 83, in parse
    for mapping in with_warn_for_invalid_lines(parse_stream(stream)):
  File "C:\..\site-packages\dotenv\main.py", line 25, in with_warn_for_invalid_lines
    for mapping in mappings:
  File "C:\..\site-packages\dotenv\parser.py", line 173, in parse_stream
    reader = Reader(stream)
  File "C:\..\site-packages\dotenv\parser.py", line 64, in __init__
    self.string = stream.read()
  File "C:\..\Python\Python39\lib\codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
binny
  • 649
  • 1
  • 8
  • 22
  • 1
    Does your .env file have a BOM? – ewokx May 10 '23 at 07:48
  • I was unfamiliar with BOM. Given I did a quick search on wikipedia, do I have to create a new `.env` file but include a `-Encoding` switch? https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8 – binny May 10 '23 at 07:53
  • 1
    I'm not sure. You just need to get rid of that BOM (if it exists). If it doesn't exist, then you have something else the problem with your .env file. – ewokx May 10 '23 at 07:54
  • That was the issue. The BOM was removed and the error has been resolved. – binny May 10 '23 at 08:02

0 Answers0