问题背景

新电脑到手,需要重新安装环境。
平时以anaconda+vscode为主,因此需要一个方便的入口来访问anaconda和python的和环境,例如vscode下面的终端就是默认使用powershell,如果能在这里访问anaconda是非常方便的。
可惜安装完anaconda后,powershell并不能直接使用conda命令。

问题描述

windows 安装 anaconda3,添加系统环境变量后,powershell不能使用命令:conda activate base

使用该命令会报错:

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run
 
    $ conda init <SHELL_NAME>
 
Currently supported shells are:
  - bash
  - 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

原因分析:

按照网上的方法,首先尝试用管理员身份运行powershell,然后运行:

PowerShell
PowerShell/PowerShell: PowerShell 是由微软开发的命令行外壳程序和脚本环境,支持任务自动化和配置管理。它包含了丰富的.NET框架功能,适用于Windows和多个非Windows平台,提供了一种强大而灵活的方式来控制和自动执行系统管理任务。
get-ExecutionPolicy   # 查看系统执行策略状态 
set-executionpolicy remotesigned # 修改执行策略状态

没有效果。


解决方案:

在运行conda init powershell时,发现会在产生一个‘C:\Users\10921\OneDrive\�ĵ�\WindowsPowerShell\profile.ps1’文件,该文件的内容为

#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
(& "D:\Programming\Anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
#endregion

猜测这是powershell的配置文件,但是不在合适的位置。
百度搜索Powershell的配置文件,发现该文件的可能的正确位置是$PROFILE.AllUsersAllHosts
我们把生成的profile.ps1文件放到这个位置(需要管理员权限),再运行conda activate base

加载个人及系统配置文件用了 1579 毫秒。
(base) PS C:\Users\10921\OneDrive\桌面> conda activate base
(base) PS C:\Users\10921\OneDrive\桌面> 

成功!!

阅读全文
AI总结
GitHub 加速计划 / po / PowerShell
31
2
下载
PowerShell/PowerShell: PowerShell 是由微软开发的命令行外壳程序和脚本环境,支持任务自动化和配置管理。它包含了丰富的.NET框架功能,适用于Windows和多个非Windows平台,提供了一种强大而灵活的方式来控制和自动执行系统管理任务。
最近提交(Master分支:8 个月前 )
7182baa3 Co-authored-by: Dongbo Wang <dongbow@microsoft.com> 18 小时前
c515887d 1 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐