【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

成功解决

相关推荐
Blossom.11819 分钟前
基于区块链的供应链溯源系统:构建与实践
人工智能·python·深度学习·机器学习·计算机视觉·flask·区块链
m0_6957362725 分钟前
python46
python
秋山落叶万岭花开ღ30 分钟前
深入探索串的高级操作:从算法到 LeetCode 实战
数据结构·python·算法
pitepa1 小时前
安装 PyCharm
ide·python·pycharm
喜欢新新子1 小时前
pycharm 中文字体报错
ide·python·pycharm
RunsenLIu1 小时前
基于Flask前后端分离智慧安防小区系统
后端·python·flask
dragon09072 小时前
Python打卡day49!!!
开发语言·python
一个天蝎座 白勺 程序猿2 小时前
Python爬虫(53)Python爬虫数据清洗与分析实战:Pandas+Great Expectations构建可信数据管道
爬虫·python·pandas
hu_nil2 小时前
Python第七周作业
java·前端·python
秋水丶秋水2 小时前
电脑桌面太单调,用Python写一个桌面小宠物应用。
开发语言·python·宠物