【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 插件解析错误

# >>>>>>>>>>>>>>>>>>>>>> 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 跳转到对应文件,可以看到这样一个函数

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
import io
  1. 函数中设置编码格式为 gb18030
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

成功解决
在这里插入图片描述

相关推荐

  1. 日常bug

    2024-03-10 23:58:02       33 阅读
  2. Delphi 编码错误问题

    2024-03-10 23:58:02       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-10 23:58:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-10 23:58:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-10 23:58:02       82 阅读
  4. Python语言-面向对象

    2024-03-10 23:58:02       91 阅读

热门阅读

  1. 探索程序员职业迷宫:选择适合自己的职业赛道

    2024-03-10 23:58:02       44 阅读
  2. 解决django跨域问题详解

    2024-03-10 23:58:02       42 阅读
  3. 关于前端的一些

    2024-03-10 23:58:02       42 阅读
  4. Leetcode Algo Day1

    2024-03-10 23:58:02       33 阅读
  5. uniapp——信号值组件(vue3)

    2024-03-10 23:58:02       41 阅读
  6. React-Context机制

    2024-03-10 23:58:02       39 阅读
  7. websocket+心跳

    2024-03-10 23:58:02       46 阅读
  8. uniapp 开发app,如何使用模拟器

    2024-03-10 23:58:02       40 阅读