【BUG修复日志】Anaconda + VSCode 编码错误

【BUG修复日志】Anaconda + VSCode 编码错误

平台: Windows11家庭版 (v22621.3155)
软件: Visual Studio Code (v1.87.0)
插件: Python (v2024.2.1)
版本: Conda (v24.1.2)

问题描述

VSCode 在安装 Python 插件的情况下自动提示配置 Conda 环境,但是在自动配置完成后启动 powershell 会报错 UnicodeEncodeError: 'gbk' codec can't encode character '\ue1bb' in position 491: illegal multibyte sequence,然而在外部中断中却不会出现,猜测是由于 Python 插件解析错误

shell 复制代码
# >>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<

    Traceback (most recent call last):
      File "D:\Softwares\Anaconda3\Lib\site-packages\conda\exception_handler.py", line 17, in __call__
        return func(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^
      File "D:\Softwares\Anaconda3\Lib\site-packages\conda\cli\main.py", line 112, in main_sourced
        print(activator.execute(), end="")
    UnicodeEncodeError: 'gbk' codec can't encode character '\ue1bb' in position 491: illegal multibyte sequence

`$ D:\Softwares\Anaconda3\Scripts\conda-script.py shell.powershell activate base`

  environment variables:
                 CIO_TEST=<not set>
        CONDA_DEFAULT_ENV=ai
                CONDA_EXE=D:\Softwares\Anaconda3\Scripts\conda.exe
             CONDA_PREFIX=D:\Softwares\Anaconda3\envs\ai
           CONDA_PREFIX_1=D:\Softwares\Anaconda3
    CONDA_PROMPT_MODIFIER=(ai)
         CONDA_PYTHON_EXE=D:\Softwares\Anaconda3\python.exe
               CONDA_ROOT=D:\Softwares\Anaconda3
              CONDA_SHLVL=2
           CURL_CA_BUNDLE=<not set>
                 HOMEPATH=\Users\heyq0
               LD_PRELOAD=<not set>
                     PATH=D:\Softwares\Anaconda3\envs\ai;D:\Softwares\Anaconda3\envs\ai\Library\
                          mingw-
                          w64\bin;D:\Softwares\Anaconda3\envs\ai\Library\usr\bin;D:\Softwares\An
                          aconda3\envs\ai\Library\bin;D:\Softwares\Anaconda3\envs\ai\Scripts;D:\
                          Softwares\Anaconda3\envs\ai\bin;D:\Softwares\Anaconda3\condabin;C:\Win
                          dows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\
                          WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;D:\Softwares\nvm;D:
                          \Softwares\nodejs;D:\Windows Kits\10\Windows Performance Toolkit;D:\So
                          ftwares\Git\cmd;D:\Softwares\寰俊web寮€鍙戣€呭伐鍏穃dll;D:\Softwares\寰俊web寮€鍙
                          戣€呭伐鍏?D:\Softwares\Anaconda3;D:\Softwares\Anaconda3\Library\bin;D:\Sof
                          twares\Anaconda3\Library\mingw-
                          w64\bin;D:\Softwares\Anaconda3\Library\usr\bin;D:\Softwares\Anaconda3\
                          Scripts;C:\Users\heyq0\AppData\Local\Microsoft\WindowsApps;D:\Software
                          s\nvm;D:\Softwares\nodejs;D:\Softwares\VisualStudioCode\bin;D:\Softwar
                          es\Anaconda3;D:\Softwares\Anaconda3\Library\mingw-
                          w64\bin;D:\Softwares\Anaconda3\Library\usr\bin;D:\Softwares\Anaconda3\
                          Library\bin;D:\Softwares\Anaconda3\Scripts;D:\Softwares\Anaconda3\bin;
                          D:\Softwares\Anaconda3\condabin;C:\Windows\system32;C:\Windows;C:\Wind
                          ows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Window
                          s\System32\OpenSSH;D:\Softwares\nvm;D:\Softwares\nodejs;D:\Windows
                          Kits\10\Windows Performance Toolkit;D:\Softwares\Git\cmd;D:\Softwares\
                          微信web开发者工具\dll;D:\Softwares\微信web开发者工具;D:\Softwares\Anaconda3;D:\Softw
                          ares\Anaconda3\Library\bin;D:\Softwares\Anaconda3\Library\mingw-
                          w64\bin;D:\Softwares\Anaconda3\Library\usr\bin;D:\Softwares\Anaconda3\
                          Scripts;C:\Users\heyq0\AppData\Local\Microsoft\WindowsApps;D:\Software
                          s\nvm;D:\Softwares\nodejs;D:\Softwares\VisualStudioCode\bin
             PSMODULEPATH=C:\Users\heyq0\Documents\WindowsPowerShell\Modules;C:\Program Files\Wi
                          ndowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Mod
                          ules
       REQUESTS_CA_BUNDLE=<not set>
            SSL_CERT_FILE=D:\Softwares\Anaconda3\envs\ai\Library\ssl\cacert.pem

     active environment : ai
    active env location : D:\Softwares\Anaconda3\envs\ai
            shell level : 2
       user config file : C:\Users\heyq0\.condarc
 populated config files : C:\Users\heyq0\.condarc
          conda version : 24.1.2
    conda-build version : 24.1.2
         python version : 3.11.7.final.0
                 solver : libmamba (default)
       virtual packages : __archspec=1=x86_64
                          __conda=24.1.2=0
                          __win=0=0
       base environment : D:\Softwares\Anaconda3  (writable)
      conda av data dir : D:\Softwares\Anaconda3\etc\conda
  conda av metadata url : None
           channel URLs : https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
          package cache : D:\Softwares\Anaconda3\pkgs
                          C:\Users\heyq0\.conda\pkgs
                          C:\Users\heyq0\AppData\Local\conda\conda\pkgs
       envs directories : D:\Softwares\Anaconda3\envs
                          C:\Users\heyq0\.conda\envs
                          C:\Users\heyq0\AppData\Local\conda\conda\envs
               platform : win-64
             user-agent : conda/24.1.2 requests/2.31.0 CPython/3.11.7 Windows/10 Windows/10.0.22621 solver/libmamba conda-libmamba-solver/24.1.0 libmambapy/1.5.6
          administrator : False
             netrc file : None
           offline mode : False


An unexpected error has occurred. Conda has prepared the above report.
If you suspect this error is being caused by a malfunctioning plugin,
consider using the --no-plugins option to turn off plugins.

Example: conda --no-plugins install <package>

Alternatively, you can set the CONDA_NO_PLUGINS environment variable on
the command line to run the command without plugins enabled.

Example: CONDA_NO_PLUGINS=true conda install <package>

问题定位

ctrl + click 跳转到对应文件,可以看到这样一个函数

python 复制代码
def main_sourced(shell, *args, **kwargs):
    """Entrypoint for the "sourced" invocation of CLI interface. E.g. `conda activate`."""
    shell = shell.replace("shell.", "", 1)

    # This is called any way later in conda.activate, so no point in removing it
    from ..base.context import context

    context.__init__()
    init_loggers()

    from ..activate import _build_activator_cls

    try:
        activator_cls = _build_activator_cls(shell)
    except KeyError:
        from ..exceptions import CondaError

        raise CondaError("%s is not a supported shell." % shell)

    activator = activator_cls(args)
    print(activator.execute(), end="")
    return 0

查阅资料可知,python 的 print 函数存在限制,不能打印全部的 unicode 字符

解决方案

  1. 文件顶部引入 io
python 复制代码
import io
  1. 函数中设置编码格式为 gb18030
python 复制代码
def main_sourced(shell, *args, **kwargs):
    """Entrypoint for the "sourced" invocation of CLI interface. E.g. `conda activate`."""
    shell = shell.replace("shell.", "", 1)

    # This is called any way later in conda.activate, so no point in removing it
    from ..base.context import context

    context.__init__()
    init_loggers()

    from ..activate import _build_activator_cls

    try:
        activator_cls = _build_activator_cls(shell)
    except KeyError:
        from ..exceptions import CondaError

        raise CondaError("%s is not a supported shell." % shell)

    activator = activator_cls(args)
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030') 
    print(activator.execute(), end="")
    return 0

成功解决

相关推荐
DanCheng-studio17 分钟前
毕设 大数据抖音短视频数据分析与可视化(源码)
python·毕业设计·毕设
易辰君1 小时前
python爬虫 - 深入requests模块
开发语言·爬虫·python
人工智障调包侠1 小时前
基于深度学习多层感知机进行手机价格预测
人工智能·python·深度学习·机器学习·数据分析
计算机编程-吉哥2 小时前
计算机毕业设计 基于Python的智能文献管理系统的设计与实现 Python+Django+Vue 前后端分离 附源码 讲解 文档
python·django·毕业设计·计算机毕业论文·计算机毕业设计选题·软件工程毕业设计论文·文献管理系统
笃励2 小时前
Java面试题二
java·开发语言·python
一颗星星辰3 小时前
Python | 第九章 | 排序和查找
服务器·网络·python
打码人的日常分享3 小时前
企业人力资源管理,人事档案管理,绩效考核,五险一金,招聘培训,薪酬管理一体化管理系统(源码)
java·数据库·python·需求分析·规格说明书
27669582923 小时前
京东e卡滑块 分析
java·javascript·python·node.js·go·滑块·京东
unix2linux3 小时前
Parade Series - SHA256
linux·python·mysql·shell
巽星石4 小时前
【Blender Python】7.一些运算、三角函数以及随机
python·blender·三角函数·随机·环形阵列