问题如下:
PS D:\codeyjj\python> conda env list
# conda environments:
#
base C:\software\anaconda3
test C:\software\anaconda3\envs\test
PS D:\codeyjj\python> conda activate test
CondaError: Run 'conda init' before 'conda activate'
PS D:\codeyjj\python> conda init
no change C:\software\anaconda3\Scripts\conda.exe
no change C:\software\anaconda3\Scripts\conda-env.exe
no change C:\software\anaconda3\Scripts\conda-script.py
no change C:\software\anaconda3\Scripts\conda-env-script.py
no change C:\software\anaconda3\condabin\conda.bat
no change C:\software\anaconda3\Library\bin\conda.bat
no change C:\software\anaconda3\condabin\_conda_activate.bat
no change C:\software\anaconda3\condabin\rename_tmp.bat
no change C:\software\anaconda3\condabin\conda_auto_activate.bat
no change C:\software\anaconda3\condabin\conda_hook.bat
no change C:\software\anaconda3\Scripts\activate.bat
no change C:\software\anaconda3\condabin\activate.bat
no change C:\software\anaconda3\condabin\deactivate.bat
no change C:\software\anaconda3\Scripts\activate
no change C:\software\anaconda3\Scripts\deactivate
no change C:\software\anaconda3\etc\profile.d\conda.sh
no change C:\software\anaconda3\etc\fish\conf.d\conda.fish
no change C:\software\anaconda3\shell\condabin\Conda.psm1
no change C:\software\anaconda3\shell\condabin\conda-hook.ps1
no change C:\software\anaconda3\Lib\site-packages\xontrib\conda.xsh
no change C:\software\anaconda3\etc\profile.d\conda.csh
no change C:\Users\eqtcj\Documents\WindowsPowerShell\profile.ps1
no change HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
No action taken.
首先手动加载 Conda 配置(注意在管理员的cmd窗口或者powershell窗口)
若初始化无效,直接加载 Conda 的 PowerShell 脚本:
. "C:\software\anaconda3\shell\condabin\conda-hook.ps1"
conda activate test
如果执行. "C:\software\anaconda3\shell\condabin\conda-hook.ps1"
报错如下:
. “C:\software\anaconda3\shell\condabin\conda-hook.ps1” . : 无法加载文件
C:\software\anaconda3\shell\condabin\conda-hook.ps1,因为在此系统上禁止运行脚本
那么按下面流程解决
方案如下:
1. 检查当前执行策略
在 PowerShell(管理员权限) 中运行:
Get-ExecutionPolicy
• 如果返回 Restricted
,说明系统禁止运行任何脚本。
2. 修改执行策略
方法 1:设置为 RemoteSigned
(推荐)
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
• 作用:允许运行本地脚本,但远程下载的脚本需签名。
• 输入 Y
确认更改。
方法 2:设置为 Unrestricted
(宽松模式,不推荐)
Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
• 作用:允许所有脚本运行(安全性较低)。
3. 验证更改
再次运行:
Get-ExecutionPolicy
• 应返回 RemoteSigned
或 Unrestricted
。
4. 重新加载 Conda 环境
尝试重新激活 Conda 环境:
. "C:\software\anaconda3\shell\condabin\conda-hook.ps1"
conda activate test
5. 其他注意事项
• 管理员权限:确保 PowerShell 以 管理员身份运行。
• 路径问题:如果 Conda 安装路径含中文或空格,建议重装到英文路径(如 C:\anaconda3
)。
• 环境变量:检查系统环境变量 PATH
是否包含 Conda 的 Scripts
和 condabin
目录。