powershell 进入conda_解决PowerShell无法使用Anaconda的问题

2023-05-16

软硬件环境

  • windows 10 64bit
  • python 3.7 with anaconda
  • conda 4.8.3

前言

前面的文章 Anaconda的基本使用 已经介绍过anaconda的基本安装及使用了,但是在windows中,如果终端工具使用的是PowerShell,会发现conda的一些命令无效

640?wx_fmt=png

powershell

当使用conda activate DCenter后,并没有进入到DCenter这个虚拟环境

640?wx_fmt=png

powershell

不过在CMD中,conda的功能是正常的

640?wx_fmt=png

powershell

解决方法

以管理员身份打开PowerShell,执行命令

conda init powershell

640?wx_fmt=png

powershell

完成后关闭PowerShell再重新打开,这时候报错了

640?wx_fmt=png

powershell

这个错误是PowerShell默认禁止脚本运行而导致的,是PowerShell的默认脚本执行策略(execution policy)。通过执行命令

get-executionpolicy

可以查看当前PowerShell的执行策略

640?wx_fmt=png

powershell

默认的执行策略是Restricted,关于这个策略,可以看看下面这张表

640?wx_fmt=png

powershell

接下来就可以修改执行策略了,执行

set-executionpolicy remotesigned

640?wx_fmt=png

powershell

上面的remotesigned是我们要修改的执行策略,关于这个策略可以参考下面这张表

640?wx_fmt=png

powershell

设置了新的策略,我们再通过get-executionpolicy来检查下是否设置成功

最后再次重新打开PowerShell,执行conda activate $虚拟环境

640?wx_fmt=png

powershell

搞定!

参考资料

  • https://github.com/conda/conda/issues/8428
  • https://xugaoxiang.com/2019/12/08/anaconda
  • https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

powershell 进入conda_解决PowerShell无法使用Anaconda的问题 的相关文章

随机推荐