Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incrroect encoding when python read file #469

Closed
star-hengxing opened this issue May 17, 2024 · 2 comments
Closed

Incrroect encoding when python read file #469

star-hengxing opened this issue May 17, 2024 · 2 comments

Comments

@star-hengxing
Copy link

if resource_exists(__name__, name):
logger.info('opening \'%s\' from packaged resource', name)
return resource_stream(__name__, name)
# fallback to filesystem
logger.info('opening \'%s\' from packaged path', name)
local_path = os.path.normpath(os.path.join(BASE_PATH, os.path.join(name)))
if not local_path.startswith(BASE_PATH):
raise ValueError
return open(local_path, *args, **kwargs)

If resource_exists failed, script will use open to read file, but local host encoding. So I got the error:

UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence

Here are some of my suggestions.

  • It's not recommended to use pkg_resources.

https://setuptools.pypa.io/en/latest/pkg_resources.html

Use of pkg_resources is deprecated in favor of importlib.resources, importlib.metadata and their backports (importlib_resources, importlib_metadata). Some useful APIs are also provided by packaging (e.g. requirements and version parsing). Users should refrain from new usage of pkg_resources and should work to port to importlib-based solutions.

  • Use PYTHONWARNDEFAULTENCODING=1 to fix potential encoding errors.
@Dav1dde
Copy link
Owner

Dav1dde commented May 20, 2024

Can you test again with the latest changes to master, would be good to know if the issues is actually fixed now.

It's not recommended to use pkg_resources.

Now it is, I think last I looked into it I needed 3.9 to make it actually work. Should update, but just a small refresher, this code technically still supports and works on Python 2.7. 3.9 is out now, so no reason to not actually use it if it's available.

@star-hengxing
Copy link
Author

It work fine, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants