解决CondaError: Run ‘conda init‘ before ‘conda activate‘

一、问题描述

创建环境:

conda create --name test_env

环境创建完成后,系统显示:

尝试激活:

conda activate test_env

输出

CondaError: Run 'conda init' before 'conda activate'

运行"conda init":

no change C:\Users\Username\miniconda3\Scripts\conda.exe no change C:\Users\Username\miniconda3\Scripts\conda-env.exe no change C:\Users\Username\miniconda3\Scripts\conda-script.py no change C:\Users\Username\miniconda3\Scripts\conda-env-script.py no change C:\Users\Username\miniconda3\condabin\conda.bat no change C:\Users\Username\miniconda3\Library\bin\conda.bat no change C:\Users\Username\miniconda3\condabin\_conda_activate.bat no change C:\Users\Username\miniconda3\condabin\rename_tmp.bat no change C:\Users\Username\miniconda3\condabin\conda_auto_activate.bat no change C:\Users\Username\miniconda3\condabin\conda_hook.bat no change C:\Users\Username\miniconda3\Scripts\activate.bat no change C:\Users\Username\miniconda3\condabin\activate.bat no change C:\Users\Username\miniconda3\condabin\deactivate.bat modified C:\Users\Username\miniconda3\Scripts\activate modified C:\Users\Username\miniconda3\Scripts\deactivate modified C:\Users\Username\miniconda3\etc\profile.d\conda.sh modified C:\Users\Username\miniconda3\etc\fish\conf.d\conda.fish no change C:\Users\Username\miniconda3\shell\condabin\Conda.psm1 modified C:\Users\Username\miniconda3\shell\condabin\conda-hook.ps1 no change C:\Users\Username\miniconda3\Lib\site-packages\xontrib\conda.xsh modified C:\Users\Username\miniconda3\etc\profile.d\conda.csh no change C:\Users\Username\Documents\WindowsPowerShell\profile.ps1 modified HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun ==> For changes to take effect, close and re-open your current shell. <==

退出 shell,重新打开并运行:

conda activate test_env

并再次得到

CondaError: Run 'conda init' before 'conda activate'

二、初步解决方法

使用 conda init powershell 或者 conda init cmd.exe

exit

再次打开命令窗口进行尝试

三、我的问题

我的power shell打开时会先报错:

Windows PowerShell 版权所有(C) Microsoft Corporation。保留所有权利。 安装最新的 PowerShell,了解新功能和改进!Migrating from Windows PowerShell 5.1 to PowerShell 7 - PowerShell | Microsoft Learn . : 无法加载文件 C:\Users\***\Documents\WindowsPowerShell\profile.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参 阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。 所在位置 行:1 字符: 3 + . 'C:\Users\***\Documents\WindowsPowerShell\profile.ps1' + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [],PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess PS

D:\workspace\Github_Projects>

错误原因:

PowerShell 试图加载用户配置文件 C:\Users\***\Documents\WindowsPowerShell\profile.ps1

但由于执行策略的限制,这个文件无法被执行

这个配置文件是由 conda init 创建的,用于设置 conda 的环境

对 conda 的影响:

会影响 conda 的正常使用

conda 的命令可能无法被正确识别

环境切换可能会失败

四、解决方法

bash 复制代码
# 设置用户级别的执行策略
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

退出后再次尝试conda activate成功!

参考: Unable to activate environment - prompted to Run 'conda init' before 'conda activate' but it doesn't work - #3 by nathanmariels - Product Help - Anaconda Community

相关推荐
YuSun_WK1 天前
使用conda配置环境时遇到的问题
conda
Caven771 天前
【Linux 技巧】如何在登录时自动激活 Conda Base 环境
linux·运维·conda
梁辰兴1 天前
PyCharm使用了Conda的虚拟环境创建的的Python项目,下载库(包)到该项目的虚拟环境中
python·pycharm·conda·错误·异常·异常报错
qq_203769492 天前
在conda环境中使用jupyter
python·jupyter·conda
中科院提名者3 天前
Conda与Pip混用导致的“幽灵包”现象
conda·pip
Victory_orsh3 天前
Conda常用命令集锦
conda
努力还债的学术吗喽6 天前
ffmpeg离线安装到服务器:解决conda/sudo/无法安装的通用方案
服务器·ffmpeg·conda
CodeLongBear7 天前
Python数据分析 -- Pandas基础入门学习笔记:从核心概念到实操代码
python·conda·pandas
小oo呆7 天前
【学习心得】conda打包进行环境迁移遇到conda包和pip包管理冲突问题
conda·pip
shixian10304119 天前
conda安装Django+pg运行环境
python·django·conda