Miniconda双终端异常:临时目录创建失败与conda命令不可用求助
问题描述
已将Miniconda重装至C盘,但仍存在以下问题:
- Anaconda Powershell Prompt仍报“Failed to create temp directory”,仅GitHub issue #9757中Ronil的方案能临时解决,但终端存在其他异常;
- Anaconda Prompt(CMD)依旧报临时目录创建失败,且
conda命令不被识别。
终端报错详情
Anaconda Powershell Prompt
Warning: PowerShell detected that you might be using a screen reader and has disabled PSReadline for compatibility purposes. If you want to re-enable it, run 'Import-Module PS Readline'. (base) PS C:\Users\king>conda usage: conda-script.py [-h] [-V] command ...
执行conda install ipykernel时还出现缓存目录错误:
(base) PS C:\Users\king> conda install ipykernel CondaError: Error encountered while attempting to create cache directory. Directory: C:\Users\king\AppData\Local\conda\conda\Cache\notices Exception: [WinEror 2] The system cannot find the file specified: 'C:\\Users\\king\\AppData\\Local\\conda\\conda\\Cache'
Anaconda Prompt(CMD)
Failed to create temp directory "C:\temp\conda-<RANDOM>\" C:\Users\king>conda 'conda' is not recognized as an internal or external command, operable program, or batch file.
疑问
- 为何两个终端报错情况不同?为何一个可用一个不可用?
- 如何修复该问题?或有无其他方法安装内核并在VSCode中运行Jupyter?
解决方案与解释
一、两个终端差异的原因
- 启动脚本逻辑不同:Anaconda Powershell Prompt的启动脚本(
conda-hook.ps1)会直接调用Miniconda的Python解释器加载conda核心,即使临时目录有问题,基础的conda命令仍能执行;而CMD的启动脚本(conda.bat)依赖C:\temp目录完成初始化,一旦该目录不存在或权限不足,脚本执行失败,导致conda的路径未被加入系统PATH,因此conda命令无法被识别。 - 目录权限与存在性差异:Powershell默认使用用户目录下的
AppData\Local\Temp作为临时目录,当前用户通常有读写权限;而CMD的启动脚本试图使用的C:\temp目录可能不存在,或者当前用户没有该目录的读写权限,导致初始化流程中断。
二、修复方案
1. 手动创建缺失的目录
- 打开文件资源管理器,创建
C:\temp目录,右键点击目录 → 属性 → 安全,确保当前用户拥有完全控制权限; - 创建
C:\Users\king\AppData\Local\conda\conda\Cache目录,同样设置当前用户的完全控制权限。
2. 配置conda的目录参数
在能运行conda的Powershell中执行以下命令,强制指定conda的临时目录、缓存目录和包存储目录:
conda config --set temp_dir C:\Users\king\AppData\Local\Temp conda config --set cache_dir C:\Users\king\AppData\Local\conda\conda\Cache conda config --set pkgs_dirs C:\Users\king\AppData\Local\conda\conda\pkgs
3. 手动修复环境变量
- 打开系统属性 → 高级 → 环境变量,在系统变量的
Path中添加Miniconda的核心目录:C:\Users\king\miniconda3C:\Users\king\miniconda3\ScriptsC:\Users\king\miniconda3\bin
- 保存后重启CMD,测试
conda --version是否正常。
4. 重新初始化conda
在Powershell中执行以下命令,重新为CMD和Powershell配置conda初始化脚本:
conda init cmd.exe conda init powershell
执行完成后关闭所有终端,重新打开即可生效。
三、替代方法:绕过conda安装Jupyter内核
如果conda仍无法正常工作,可以直接使用Python的pip工具安装ipykernel,步骤如下:
- 打开Powershell,手动激活Miniconda的base环境:
& C:\Users\king\miniconda3\Scripts\activate.ps1
- 安装ipykernel并注册到Jupyter:
pip install ipykernel python -m ipykernel install --user --name base --display-name "Python (Miniconda Base)"
- 打开VSCode,安装Jupyter扩展,创建或打开Jupyter笔记本后,在右上角的内核选择菜单中选择
Python (Miniconda Base)即可运行代码。
内容的提问来源于stack exchange,提问作者JustPeter
相关产品推荐
相关产品推荐

