VS Code启动时终端Python虚拟环境激活显示vscode conda activate xxx CommandNotFoundError...

Windows 10运用VS Code进行Python程序开发,将当前环境设为某个虚拟环境时(本例为tfgpu, 即tensorflow的GPU环境):

 

(编辑时切换到tfgpu虚拟环境的截图) 

当下次启动,默认将在Terminal(终端窗口)会通过conda activate xxx  启动该虚拟环境, 这时会出现如下错误:

vscode conda activate tfgpu  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'.

后面提供了其它一些说明。

即使在环境变量中设置了anaconda执行路径(需要注意的是:Windows 10系统环境变量设置之后,需要重启系统,VS Code终端才能更新环境参数与路径),依然会出现该错误。  

参考该文(python - Windows 10 VSCode 激活 conda 虚拟环境失败_个人文章 - SegmentFault 思否)后,解决方法如下(本人采用Windows 10 Pro系统,个人即管理员权限, VSCode Terminal默认Shell为Windows Powershell):  

在VS Code终端窗口中执行:  

conda init 

重启VS Code, 执行下面命令: 

set-ExecutionPolicy RemoteSigned

再重新启动后,Python虚拟环境即可以正常激活了。 

困扰了一段时间终于解决(虽然没深究机理),感谢文中链接作者,特此记录。 

猜你喜欢

转载自blog.csdn.net/cloudflash/article/details/127609433