UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Hi there everyone!

I’m new to this forum, although I’ve been using Streamlit for 3 months or so. I’ve run into a problem that I don’t know how to solve. I’ve been working in a streamlit app that before yesterday worked just fine. Somehow, it doesn’t work anymore and I’m not conscious of having done anything that could alter its behaviour.

The error is this one:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

So I surfed on the internet reading why this happens and there are a lot of topics related to csv files and so on. I read separately each of the csv files and excel files that my app uses and the problem isn’t there.

Then I created a dumb app that consists in this two lines of code with the only purpose of executing a streamlit app to see if the error was there:

import streamlit as st

st.write("Hi there!")

And the error that I got is exactly the same:

Traceback (most recent call last):
  File "C:/Users/alons/PycharmProjects/maps5/prueba2.py", line 3, in <module>
    st.write("Hi there!")
  File "C:\Users\anaconda3\envs\maps5\lib\site-packages\streamlit\elements\write.py", line 234, in write
    flush_buffer()
  File "C:\Users\anaconda3\envs\maps5\lib\site-packages\streamlit\elements\write.py", line 168, in flush_buffer
    self.dg.markdown(
  File "C:\Users\anaconda3\envs\maps5\lib\site-packages\streamlit\elements\markdown.py", line 79, in markdown
    return self.dg._enqueue("markdown", markdown_proto)
  File "C:\Users\anaconda3\envs\maps5\lib\site-packages\streamlit\delta_generator.py", line 376, in _enqueue
    st._maybe_print_use_warning()
  File "C:\Users\anaconda3\envs\maps5\lib\site-packages\streamlit\__init__.py", line 466, in _maybe_print_use_warning
    elif not _is_running_with_streamlit and _config.get_option(
  File "C:\Users\anaconda3\envs\maps5\lib\site-packages\streamlit\config.py", line 103, in get_option
    config_options = get_config_options()
  File "C:\Users\anaconda3\envs\maps5\lib\site-packages\streamlit\config.py", line 1096, in get_config_options
    file_contents = input.read()
  File "C:\Users\anaconda3\envs\maps5\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

If I run “Streamlit hello” on the terminal, once more I get the same error.

I don’t know why this happens, everything worked just fine until yesterday.

Streamlit version when I encountered this error: I think 1.4, then upgraded to 1.7 (didn’t work) then downgraded to 1.2 (didn’t work).
I’m using a conda environment and python version 3.9.0 in a Windows machine.

Thanks in advance!

Hi @Alonso -

I would check what codec your text editor is set to save files as. I suspect the issue, since you are using a Windows computer, is that the source Python file is being set to something other than UTF-8. Switch that encoding and try again and see if that fixes your issue.

Best,
Randy

Hi @randyzwitch, thanks for yor answer!

I’m using pycharm as code editor and my configuration was this one:

I changed that windows-1252 to UTF-8 and that ISO-8859-1 to UTF-8 as well. Saved my files, reinitiated pycharm and it doesn’t work.

What surprises me is that it seems some problem related to Streamlit, because I can run other files, but not the ones that make use of Streamlit.

Thanks again!

I am pretty sure that this issue is not related to streamlit in any way.
Please check your file encoding with a different editor (VSCode, Notepad++ …)

2 Likes

Hi, in the case of streamlit it’s a matter of configuring your folder, something you’ll have to do manually.
You go to that streamlit folder and delete the files inside it,
image
normally there are 3 configuration files, after that I recommend you to uninstall the library close the terminal, install again.

1 Like

Wow thanks, that worked!

Thank you very much!

i have tried with that metod but still have this error:
Traceback (most recent call last):
File “”, line 198, in _run_module_as_main
File “”, line 88, in run_code
File "C:\Users\pietr\AppData\Local\Programs\Python\Python311\Scripts\streamlit.exe_main
.py", line 7, in
File “C:\Users\pietr\AppData\Roaming\Python\Python311\site-packages\click\core.py”, line 1130, in call
return self.main(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\pietr\AppData\Roaming\Python\Python311\site-packages\click\core.py”, line 1055, in main
rv = self.invoke(ctx)
^^^^^^^^^^^^^^^^
File “C:\Users\pietr\AppData\Roaming\Python\Python311\site-packages\click\core.py”, line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\pietr\AppData\Roaming\Python\Python311\site-packages\click\core.py”, line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\pietr\AppData\Roaming\Python\Python311\site-packages\click\core.py”, line 760, in invoke
return __callback(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\pietr\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\web\cli.py”, line 174, in main_run
bootstrap.load_config_options(flag_options=kwargs)
File “C:\Users\pietr\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\web\bootstrap.py”, line 315, in load_config_options
config.get_config_options(force_reparse=True, options_from_flags=options_from_flags)
File “C:\Users\pietr\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\config.py”, line 1091, in get_config_options
file_contents = input.read()
^^^^^^^^^^^^
File “”, line 322, in decode
UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xff in position 0: invalid start byte

I use the ‘st_files_connection’ library to access a CSV file in Google Cloud. I have tried changing the encoding of the .csv file in my bucket (UTF-8, UTF-8-SIG) in every way possible, but I still encounter the UnicodeDecodeError: ‘charmap’ codec can’t decode byte 0x8d" error.

I’m experiencing the same issue. I attempted to remove all files and uninstall Streamlit. Upon opening the folder, I observed that the files were successfully removed. Subsequently, I tried reinstalling the package using the command: pip install --upgrade --force-reinstall streamlit. I observed that the files were being installed, however, the problem persists.

The error within the config file appears to be associated with the following lines:

        for filename in CONFIG_FILENAMES:
            if not os.path.exists(filename):
                continue

            with open(filename, "r", encoding="utf-8") as input:
                file_contents = input.read()

            _update_config_with_toml(file_contents, filename)

        _update_config_with_sensitive_env_var(_config_options)

Edit:

I’ve managed to find a solution, and it appears that everything is now running smoothly. I simply navigated to the line in the config.py file that reads:

with open(filename, "r", encoding="utf-8") as input:

And made the following change:

with open(filename, "r", encoding="ISO-8859-1") as input: