Windows10中Conda环境下Jupyter执行命令报错:& was unexpected at this time
解决Windows 10下Conda环境Jupyter Notebook中
!pip list/%pip list报错问题 问题原因
该错误通常是Windows cmd解析带空格/特殊字符的路径时出现语法错误,或是Jupyter与Conda环境的pip关联异常导致。
解决方案
1. 直接调用pip的绝对路径
先在激活的Conda环境的CMD窗口执行where pip,获取pip的完整路径(例如D:\miniconda3\envs\your_env\Scripts\pip.exe),然后在Jupyter Notebook中执行:
!D:\miniconda3\envs\your_env\Scripts\pip.exe list
2. 切换Jupyter的默认Shell为PowerShell
Windows cmd对空格路径的处理存在缺陷,切换到PowerShell可解决:
- 临时切换:在Notebook中先执行
!set SHELL=powershell,再运行!pip list - 永久配置:
- 激活Conda环境,执行
jupyter notebook --generate-config生成配置文件 - 打开生成的
jupyter_notebook_config.py(通常在C:\Users\你的用户名\.jupyter目录下) - 添加或修改以下配置:
c.NotebookApp.shell = 'powershell' c.NotebookApp.terminado_settings = {'shell_command': ['powershell.exe']} - 重启Jupyter Notebook
- 激活Conda环境,执行
3. 重建Conda环境到无空格路径
如果你的Conda环境路径包含空格(比如C:\Users\你的 用户名\miniconda3\envs\...),建议重新创建环境到无空格路径:
conda create -n your_env python=3.9.15 --prefix D:\conda_envs\your_env conda activate D:\conda_envs\your_env conda install jupyter notebook -y
之后启动Jupyter再执行%pip list即可。
4. 重新关联Jupyter与Conda环境
若Jupyter未正确绑定当前Conda环境的pip,执行以下步骤:
- 激活目标Conda环境:
conda activate your_env - 卸载现有Jupyter组件:
pip uninstall jupyter jupyter_core jupyter_client -y - 用Conda重新安装Jupyter:
conda install jupyter notebook -y - 启动Jupyter:
jupyter notebook
内容的提问来源于stack exchange,提问作者Sabbir Hasan Munna
相关产品推荐
相关产品推荐

