在使用vs code下conda环境,运行程序会报告如下问题
conda activate base
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'.
网上查找后解决办法如下,方法二亲测可用:
方法一:
使用anaconda自带的anaconda navigator或anaconda powershell prompt,anaconda prompt来进入环境,或输入conda.bat activate “环境路径”,例如conda.batactivate"D:\Anaconda3\envs\test"
方法二:
第一步:
打开管理员power shell,修改执行策略,执行:get-ExecutionPolicy,回复Restricted,表示状态是禁止的
Restricted——默认的设置, 不允许任何script运行
AllSigned——只能运行经过数字证书签名的script
RemoteSigned——运行本地的script不需要数字签名,但是运行从网络上下载的script就必须要有数字签名
Unrestricted——允许所有的script运行
第二步:
执行:set-ExecutionPolicy RemoteSigned即可
第三步:
输入:conda init
此时:C:\Users\xxx\Documents\WindowsPowerShell下会生成配置文件profile.ps1
重启power shell或者vs code后,默认会进入conda的base环境
恢复:
删除profile.ps1。
根据个人情况,执行set-ExecutionPolicy Restricted策略改回Restricted。