arviz-devs / arviz

Exploratory analysis of Bayesian models with Python
https://python.arviz.org
Apache License 2.0
1.6k stars 397 forks source link

UnicodeDecodeError when i import arivz #2274

Closed fish-galaxy closed 9 months ago

fish-galaxy commented 1 year ago

Holle, I want to run:

import arviz

the console return:
  File ~\anaconda3\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\mypy\hann-hydro\ces.py:7
    import arviz as az

  File ~\anaconda3\lib\site-packages\arviz\__init__.py:33
    from .data import *

  File ~\anaconda3\lib\site-packages\arviz\data\__init__.py:2
    from .base import CoordSpec, DimSpec, dict_to_dataset, numpy_to_data_array

  File ~\anaconda3\lib\site-packages\arviz\data\base.py:20
    from .. import __version__, utils

  File ~\anaconda3\lib\site-packages\arviz\utils.py:666
    class HtmlTemplate:

  File ~\anaconda3\lib\site-packages\arviz\utils.py:691 in HtmlTemplate
    _, css_style = _load_static_files()  # pylint: disable=protected-access

  File ~\anaconda3\lib\site-packages\arviz\utils.py:663 in _load_static_files
    return [importlib.resources.files("arviz").joinpath(fname).read_text() for fname in STATIC_FILES]

  File ~\anaconda3\lib\site-packages\arviz\utils.py:663 in <listcomp>
    return [importlib.resources.files("arviz").joinpath(fname).read_text() for fname in STATIC_FILES]

  File ~\anaconda3\lib\pathlib.py:1135 in read_text
    return f.read()

UnicodeDecodeError: 'gbk' codec can't decode byte 0xba in position 2367: illegal multibyte sequence

What do I need to do to solve this problem? Looking forward to your answer!

ahartikainen commented 1 year ago

Hi, what python version and arviz version do you have?

fish-galaxy commented 1 year ago

I use the version is python 3.10.9 and arviz 0.16.1

sethaxen commented 1 year ago

This looks very similar, perhaps identical, to an issue that came up in CI when wrapping arviz with Julia (https://github.com/JuliaRegistries/General/pull/88578#issuecomment-1671179381). Note that the error traces back to the same line of Python code: https://github.com/JuliaRegistries/General/actions/runs/5808545587/job/15745534758#step:16:885 . This was with a fresh Conda environment built with micromamba with only the constraint "arviz >= 0.14.0".

vitwei commented 1 year ago

similar question UnicodeDecodeError: 'gbk' codec can't decode byte 0xba in position 2367: illegal multibyte sequence

import arviz Traceback (most recent call last): File "", line 1, in File "D:\anacoda\envs\pymc4\Lib\site-packages\arvizinit.py", line 33, in from .data import * File "D:\anacoda\envs\pymc4\Lib\site-packages\arviz\datainit.py", line 2, in from .base import CoordSpec, DimSpec, dict_to_dataset, numpy_to_dataarray File "D:\anacoda\envs\pymc4\Lib\site-packages\arviz\data\base.py", line 20, in from .. import version, utils File "D:\anacoda\envs\pymc4\Lib\site-packages\arviz\utils.py", line 667, in class HtmlTemplate: File "D:\anacoda\envs\pymc4\Lib\site-packages\arviz\utils.py", line 692, in HtmlTemplate , css_style = _load_static_files() # pylint: disable=protected-access ^^^^^^^^^^^^^^^^^^^^ File "D:\anacoda\envs\pymc4\Lib\site-packages\arviz\utils.py", line 662, in _load_static_files return [ ^ File "D:\anacoda\envs\pymc4\Lib\site-packages\arviz\utils.py", line 663, in importlib.resources.files("arviz").joinpath(fname).read_text() for fname in STATIC_FILES ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\anacoda\envs\pymc4\Lib\pathlib.py", line 1059, in read_text return f.read() ^^^^^^^^ UnicodeDecodeError: 'gbk' codec can't decode byte 0xba in position 2367: illegal multibyte sequence

python 3.11.4 arivz 0.16.0

using arivz 0.15 non error

fish-galaxy commented 1 year ago

@sethaxen Thanks, i find arviz can successfully run if the version<0.16.0 when i try to change the version. @vitwei You can try to ues the arviz0.15.1 or lower version.

ahartikainen commented 1 year ago

How did you install arviz?

Conda or pip?

lucianopaz commented 9 months ago

This also came up here. The issue there was that the user was on windows. The system encoding there isn't utf-8, if I remember correctly it's Latin1, so when they try to read the contents of the file that has utf-8 special characters, the system level encoding can't understand them and errors out. To fix that, you'll need to explicitly pass encoding="utf-8" when calling read_text.

OriolAbril commented 9 months ago

We need to add the encoding argument in https://github.com/arviz-devs/arviz/blob/6b1b2be60cca804d4b0ec43a3303820bfa8785c6/arviz/utils.py#L671 then. Thanks @lucianopaz!