Conda激活环境无效

问题如下:

bash 复制代码
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 脚本:

bash 复制代码
. "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(管理员权限) 中运行:

powershell 复制代码
Get-ExecutionPolicy

• 如果返回 Restricted,说明系统禁止运行任何脚本。


2. 修改执行策略
方法 1:设置为 RemoteSigned(推荐)

powershell 复制代码
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

• 作用:允许运行本地脚本,但远程下载的脚本需签名。

• 输入 Y 确认更改。

方法 2:设置为 Unrestricted(宽松模式,不推荐)

powershell 复制代码
Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force

• 作用:允许所有脚本运行(安全性较低)。


3. 验证更改

再次运行:

powershell 复制代码
Get-ExecutionPolicy

• 应返回 RemoteSignedUnrestricted


4. 重新加载 Conda 环境

尝试重新激活 Conda 环境:

powershell 复制代码
. "C:\software\anaconda3\shell\condabin\conda-hook.ps1"
conda activate test

5. 其他注意事项

• 管理员权限:确保 PowerShell 以 管理员身份运行。

• 路径问题:如果 Conda 安装路径含中文或空格,建议重装到英文路径(如 C:\anaconda3)。

• 环境变量:检查系统环境变量 PATH 是否包含 Conda 的 Scriptscondabin 目录。

相关推荐
砍材农夫4 天前
python环境|conda安装和使用(1)
开发语言·后端·python·conda
匆匆整棹还4 天前
mamba的安装和版本对应
conda
2601_961875245 天前
花生十三资料1200题|题库|刷题
conda·pytest·pillow·pip·web3.py·ipython·gunicorn
没有钱的钱仔6 天前
自动创建conda虚拟环境,并安装依赖包
conda
砍材农夫7 天前
python环境|pip|uv|venv|Conda区别
后端·python·conda·pip·uv
pixelpilot18 天前
Conda:跨平台的二进制包管理器
其他·conda
lg_cool_8 天前
使用conda管理python运行环境并关联vscode
vscode·python·conda
javajenius8 天前
Pixi:用 Rust 重写 Conda 体验的包管理工具
开发语言·其他·rust·conda
小白弄潮儿9 天前
Conda 使用入门指南
conda
DFT计算杂谈9 天前
WannierTools输入文件wt.in一键批量生成脚本
java·前端·chrome·python·算法·conda