解决:‘utf-8’ codec can’t decode byte(本质是from scipy.misc import imread问题)

在尝试使用pip安装imread时遇到'utf-8'编码错误,其实是由于from scipy.misc import imread等方法已被弃用。正确解决方案是使用imageio库。方法包括:1) 安装imageio并用其替代scipy的imread;2) 降级scipy版本到1.2.1;3) 修改python的编码方式为gbk;4) 以管理员权限运行cmd或AnacondaPrompt。此外,还提供了其他可能的解决途径,如使用GitBash。
部署运行你感兴趣的模型镜像

一、问题

pip 时 ‘utf-8’ codec can’t decode byte

pip install imread 的过程中遇到了这个问题

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

image-20210819172220998

二、思路

一开始查到的各种解决方案都是编码上的问题,后来才发现原来是思路错了。
这个问题其实是因为 from scipy.misc import imread,imwrite 等方法已经被弃用。
Python已经将imread方法封装在了imageio模块当中。
因此方法三解决了我的问题,应该通过imageio模块来调取imread而不再是从scipy.misc中调取。
不过以下还是会提供三个方法

三、解决

1、方法一:版本中已移除该方法(有效)

如果是在以下语句中报错,可能是版本问题,这句调用代码已经被移除

from scipy.misc import imread,imsave

解决方案如下:

# 1.安装 imageio 库
pip install imageio
# 2.在python中
from imageio import imread
# 3.代码中调用方式
imread("xxxx.png")

参考博客:Python中from scipy.misc import imread报错的原因?

2、方法二:降低spicy版本(有效)

pip install scipy==1.2.1

这样也是可以的

3、方法三:修改编码方式

因为 python3 默认编码是unicode;python2是ASCII码;windows环境默认是gbk编码

修改 python安装目录下\Lib\site-packages\pip\compat的_init_.py,搜索 def console_to_str(s) 函数,修改代码为

def console_to_str(s):
    try:
        return s.decode(sys.__stdout__.encoding)
    except UnicodeDecodeError:
    	# 或者也可以尝试改为 cp936、GB2312、gb18030
        return s.decode('gbk')

image-20210819203639903

参考博客:

pip错误:‘utf-8’ codec can’t decode byte解决方法

UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xb2 in position 99: invalid start byte

关于Python字符编码encode和decode

4、方法四:管理员运行

以管理员身份运行 cmd 或者 Anaconda Prompt

参考博客:Pip安装pyinstaller出现utf-8解码错误

5、其他(未尝试)

如用 Git Bush 命令行安装

Python pip 安装报错;‘utf-8’ codec can’t decode byte 0xce in position 72

windows下使用GitBash命令工具执行python文件

您可能感兴趣的与本文相关的镜像

Python3.9

Python3.9

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

-------------------------------------------------------------------------- InvalidDataError Traceback (most recent call last) File /usr/local/lib/python3.10/site-packages/imageio/plugins/pyav.py:308, in PyAVPlugin.__init__(self, request, container, **kwargs) 307 else: --> 308 self._container = av.open(request.get_file(), **kwargs) 309 self._video_stream = self._container.streams.video[0] File av/container/core.pyx:374, in av.container.core.open() File av/container/core.pyx:239, in av.container.core.Container.__cinit__() File av/container/core.pyx:259, in av.container.core.Container.err_check() File av/error.pyx:428, in av.error.err_check() InvalidDataError: [Errno 1094995529] Invalid data found when processing input: '<none>' During handling of the above exception, another exception occurred: AttributeError Traceback (most recent call last) File /usr/local/lib/python3.10/site-packages/imageio/core/imopen.py:138, in imopen(uri, io_mode, plugin, extension, format_hint, legacy_mode, **kwargs) 137 try: --> 138 return loader(request, **kwargs) 139 except InitializationError as class_specific: File /usr/local/lib/python3.10/site-packages/imageio/core/imopen.py:130, in imopen.<locals>.loader(request, **kwargs) 129 def loader(request, **kwargs): --> 130 return config.plugin_class(request, **kwargs) File /usr/local/lib/python3.10/site-packages/imageio/plugins/pyav.py:311, in PyAVPlugin.__init__(self, request, container, **kwargs) 310 self._decoder = self._container.decode(video=0) --> 311 except av.AVError: 312 if isinstance(request.raw_uri, bytes): AttributeError: module 'av' has no attribute 'AVError' The above exception was the direct cause of the following exception: OSError Traceback (most recent call last) Cell In[8], line 39 32 user_msg = BaseMessage.make_user_message( 33 role_name="User", 34 content=
03-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值