powershell 7使用 conda 的 BUG /问题

1. 在powershell 7 中启用conda

conda init powershell

输入后可以启用环境,但是(1)不显示环境,(2)报错误。

2. 错误报告

一句话总结:自作聪明的Windows在读取.py文件时采用了GBK编码而非UTF-8,但是powershell 7的默认编码是UTF-8,因此冲突。有powershell7默认属性为证:
powershell7 默认属性
错误日志:

 JustinZhang@ZYF  ~ 
❯ conda deactivate

# >>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<

    Traceback (most recent call last):
      File "F:\ProgramFiles\Anaconda3\lib\site-packages\conda\cli\main.py", line 140, in main
        return activator_main()
      File "F:\ProgramFiles\Anaconda3\lib\site-packages\conda\activate.py", line 1210, in main
        print(activator.execute(), end='')
    UnicodeEncodeError: 'gbk' codec can't encode character '\ufffd' in position 930: illegal multibyte sequence

……(此处略过诊断日志里各种配置信息)

An unexpected error has occurred. Conda has prepared the above report.

If submitted, this report will be used by core maintainers to improve
future releases of conda.
Would you like conda to send this report to the core maintainers?

yes
Upload successful.

Thank you for helping to improve conda.
Opt-in to always sending reports (and not see this message again)
by running

    $ conda config --set report_errors true


Invoke-Expression: F:\ProgramFiles\Anaconda3\shell\condabin\Conda.psm1:135
Line |
 135 |          Invoke-Expression -Command $deactivateCommand;
     |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Missing ] at end of attribute or type literal.

解决方法1:修理powershell7

利用.ps1配置文件设置powershell7为GBK编码。

code $PROFILE

在打开的.ps1文件中,添加如下行:

#-------------------------------     Set    Coding    -------------------------------
<# 936代表GBK,65001代表UTF-8。可以根据需要选择执行,将另一行注释掉即可。
#>
[System.Console]::OutputEncoding=[System.Text.Encoding]::GetEncoding(936)
[System.Console]::OutputEncoding=[System.Text.Encoding]::GetEncoding(65001)
#-------------------------------   Set  Coding   End  -------------------------------

重新打开powershell7即可。

解决方法2:配置注册表

解决方法3:更改Windows系统设置

** 总之,各种方法的原则相同:配置powershell与windows系统相同编码,要么改Powershell7,要么改windows。 **

关于方法2和方法3请参见:https://www.zhihu.com/question/54724102。必须说明,我本人没有使用这两个方法,因为评论里有人说这样会让其他程序乱码…… 就很烦,因为windows上软件诸多,每个软件编码不同,一刀切处理不太妥当。比如VMware的配置文件
默认就是GBK编码。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值