anaconda -spyder报错解决-UnicodeDecodeError: 'gbk' codec can't decode byte 0x93 in position 611: illegal

在使用Anaconda的Spyder编写Python程序时遇到'gbk'编码错误,报错UnicodeDecodeError。尝试重启程序无效,解决方法是根据错误提示修改文件编码设置。
部署运行你感兴趣的模型镜像

此文首发于我的个人博客:anaconda -spyder报错解决-UnicodeDecodeError ‘gbk’ codec can’t decode byte 0x93 in position 611 illegal — zhang0peter的个人博客


下午在写Python程序时突然遇到报错:

This command failed to be executed because an error occurred while trying to get the file code from Spyder's editor. The error was:


An exception has occurred, use %tb to see the full traceback.

UnicodeDecodeError: 'gbk' codec can't decode byte 0x93 in position 582: illegal multibyte sequence

UnicodeDecodeError: 'gbk' codec can't decode byte 0x93 in position 611: illegal multibyte sequence

In [2]: %tb
Traceback (most recent call last):

  File "C:\Users\peter\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 401, in get_file_code
    return f.read()

UnicodeDecodeError: 'gbk' codec can't decode byte 0x93 in position 582: illegal multibyte sequence

先尝试重启程序,也许报错就解决了

先尝试重启程序,也许报错就解决了

解决方法是根据报错,修改文件C:\Users\peter\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py为:

def get_file_code(filename):
    """Retrive the content of a file."""
    # Get code from spyder
    try:
        file_code = frontend_request().get_file_code(filename)
    except (CommError, TimeoutError):
        file_code = None
    if file_code is None:
        with open(filename, 'r',encoding='utf-8') as f:#增加 ,encoding='utf-8'
            return f.read()
    return file_code

如果不重启程序,会报错:

[autoreload of spydercustomize failed: Traceback (most recent call last):
  File "C:\Users\peter\Anaconda3\lib\site-packages\IPython\extensions\autoreload.py", line 245, in check
    superreload(m, reload, self.old_objects)
  File "C:\Users\peter\Anaconda3\lib\site-packages\IPython\extensions\autoreload.py", line 394, in superreload
    module = reload(module)
  File "C:\Users\peter\Anaconda3\lib\imp.py", line 314, in reload
    return importlib.reload(module)
  File "C:\Users\peter\Anaconda3\lib\importlib\__init__.py", line 168, in reload
    raise ModuleNotFoundError(f"spec not found for the module {name!r}", name=name)
ModuleNotFoundError: spec not found for the module 'spydercustomize'
]
This command failed to be executed because an error occurred while trying to get the file code from Spyder's editor. The error was:


An exception has occurred, use %tb to see the full traceback.

UnicodeDecodeError: 'gbk' codec can't decode byte 0x93 in position 582: illegal multibyte sequence

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

Python3.8

Python3.8

Conda
Python

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

### Conda 中出现 `UnicodeDecodeError` 错误的原因 在使用 Conda 的过程中,如果遇到类似于 `UnicodeDecodeError: 'gbk' codec can't decode byte` 的错误,通常是因为系统默认使用的 GBK 编码无法正确解析某些特殊字符。这种问题可能发生在 Windows 平台上,因为其默认编码为 GBK 而不是 UTF-8。 #### 错误原因分析 此错误的根本原因是 Python 或者操作系统尝试用 GBK 编码去解读一个包含非 GBK 支持字符的数据流或文件内容。当这些数据中存在超出 GBK 字符集范围的字节序列时,就会抛出上述异常[^1]。具体场景包括但不限于: - 文件路径中含有特殊字符。 - 使用了不兼容的编码格式读写文件。 - Anaconda Prompt 默认编码设置不当。 #### 解决方案 以下是几种常见的解决方案: 1. **修改系统的代码页** 可通过命令行更改当前终端会话所采用的文字编码标准至更广泛的 UTF-8 来规避此类冲突情况的发生。操作如下: ```cmd chcp 65001 ``` 此处,“chcp”代表改变活动控制台窗口中的代码页。“65001”即表示切换成 UTF-8 编码模式[^4]。 2. **调整环境变量配置** 设置 PYTHONIOENCODING 环境变量强制指定输入输出均基于 utf-8 处理,从而避免潜在的编码转换失败风险。 ```bash set PYTHONIOENCODING=utf-8 ``` 3. **更新 conda 配置项** 如果以上两种方法仍未能有效解决问题,则考虑直接编辑 .condarc 文件加入 ignore encode errors 参数选项以忽略掉那些难以处理的部分。 ```yaml channels: - defaults ssl_verify: true extra_safety_checks: false allow_other_channels: true always_yes: false changeps1: false channel_priority: flexible anaconda_upload: null remote_connect_timeout_secs: 9.15 client_ssl_cert: None client_ssl_key: None proxy_servers: {} report_errors: true safety_checks: warn show_channel_urls: none update_specifier_match_exact_version_numbers_in_specs_file: false use_pip: True auto_update_conda: False add_anaconda_token: True repodata_threads: automatic sat_solver: pycosat verify_threads: 5 solver_ignore_packages: [] disallowed_packages: [] allowed_channels: ['defaults'] default_python: 3.7 non_admin_enabled: True path_conflict: clobber aggressive_update_packages: - ca-certificates - certifi - openssl restore_free_channel: False notify_outdated_conda: True download_only: False offline_mode: False quiet: False verbosity: 0 json: False dry_run: False debug: False trace: False context plugged from data sources: [CLI, ENV] ignore_encode_errors: true ``` 4. **升级 Conda 版本** 确保安装的是最新版本的 Conda ,新版本往往修复了许多旧版中存在的 bug 和性能优化改进等问题 。可以通过运行下面这条指令完成自我迭代升级过程 : ```bash conda update conda ``` 5. **替换特定函数调用参数** 对于程序内部引发的相关异常情形 ,可以在涉及字符串操作的地方显式声明期望遵循的目标编码形式比如 utf-8 如下所示例子展示了如何安全地打开并加载外部资源而不受本地化影响. ```python with open('example.txt','r',encoding='utf-8') as file_object: contents = file_object.read() ``` ### 总结 综上所述,针对 Conda 抛出 `'gbk' codec can't decode byte` 类型的错误信息,可以从多个角度切入寻找对应的处置策略。无论是临时性的快速修正还是长期稳定的架构改造都有相应的技术手段可供选择应用实践当中。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值