Python终端激活环境报错解决方法:重启shell或运行‘conda init‘

Python运行conda环境报错You may need to close and restart your shell after running ‘conda init’

第一次在Python用conda环境运行代码,点击运行后报错IMPORTANT: You may need to close and restart your shell after running ‘conda init’.

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.
 
To initialize your shell, run
 
    $ conda init <SHELL_NAME>
 
Currently supported shells are:
  - bash
  - cmd.exe
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell
 
See 'conda init --help' for more information and options.
 
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
 

##初步判断是安全问题,在网页找到了一个有效的方法。先以管理员方式打开cmd控制台输入下面两条命令

Get-ExecutionPolicy   #以下命令来查看当前的执行策略
Set-ExecutionPolicy RemoteSigned   #设置执行策略

‘Set-ExecutionPolicy’ 命令将执行策略设置为 ‘RemoteSigned’,这将允许运行由信任的发布者签名的脚本,以及在本地创建的脚本

再次运行代码就正常了

总结:这个报错是因为在你的系统上,PowerShell 的执行策略不允许运行脚本。在 PowerShell 中,执行策略是由 Microsoft 提供的安全机制,用于控制你可以如何执行脚本。执行策略更改为 RemoteSigned,这意味着任何从互联网下载的脚本都需要被签名,但是你本地创建的脚本可以被正常执行。

物联沃分享整理
物联沃-IOTWORD物联网 » Python终端激活环境报错解决方法:重启shell或运行‘conda init‘

发表回复