vs code中jupyter内核启动失败主因是jupyter命令未安装或未加入path、python解释器未正确选择、端口被占或防火墙拦截;需先运行jupyter --version验证,再通过python: select interpreter指定环境,重启vs code并清理~/.jupyter/runtime/目录。

kernel 启动失败:找不到 Python 解释器或 jupyter 命令
VS Code 的 Jupyter 插件本质依赖本地已安装的 jupyter 可执行文件(通常是 jupyter-notebook 或 jupyter-lab 后端),如果它根本没装、没加进 PATH,或者你用的是 conda 虚拟环境但没激活,就会卡在 “Starting Jupyter Server…” 或报错 Command 'jupyter' not found。
实操建议:
- 终端里直接运行
jupyter --version,确认能输出版本号;不能?先装:用pip install jupyter或conda install jupyter - VS Code 里按
Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(macOS),输入Python: Select Interpreter,选中你希望 Jupyter 使用的那个 Python 环境(比如./venv/bin/python或~/miniconda3/envs/myenv/bin/python)——这步必须做,否则插件会默认找系统 Python,而那里很可能没装jupyter - 重启 VS Code,再打开 .ipynb 文件,看右上角 kernel 选择器是否显示“Python 3 (myenv)”之类;若仍灰显,点它手动选一次
内核连接超时或反复断开:Failed to start the Kernel
常见于网络代理、防火墙拦截 localhost 通信,或 Jupyter 启动时绑定的端口被占。VS Code 默认用 --no-browser --port=0 启动内核,但某些企业环境或 Docker 容器里会失败。
实操建议:
- 检查终端是否有残留的
jupyter进程:ps aux | grep jupyter(macOS/Linux)或任务管理器搜python.exe,杀掉旧进程 - 临时关闭代理软件(如 Clash、Surge)和 Windows Defender 防火墙(测试用)
- 在 VS Code 设置里搜索
jupyter: allow unauthorized remote connections,勾选它(仅限可信本地环境) - 如果用 conda,确保没混用 pip 和 conda 安装 jupyter:统一用
conda install ipykernel,再运行python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
单元格运行无响应,输出区空白或只显示 * * *
不是代码卡死,而是前端没收到 kernel 返回——大概率是 kernel 实际已启动,但 VS Code 没正确识别其通信通道(比如 kernel.json 里指定的 argv 路径错误,或 zmq 协议不匹配)。
实操建议:
- 打开命令面板,执行
Jupyter: Restart Kernel and Clear All Outputs,别只点“Restart Kernel”,要带 Clear - 查看 VS Code 底部状态栏右侧:如果有黄色警告图标,悬停看提示,常是
Kernel connection failed,此时点它会弹出日志路径,打开对应jupyter.log查最后一段 ERROR - 删掉用户目录下的
~/.jupyter/runtime/(Linux/macOS)或%USERPROFILE%\.jupyter\runtime\(Windows),强制重建连接文件 - 禁用所有非必要插件(尤其那些改终端行为的),只留 Python + Jupyter 插件,排除干扰
使用远程内核(SSH / WSL2)时无法连接
WSL2 的 IP 是动态的,SSH 端口转发没配对,或远程机器上没装 ipykernel,都会导致 VS Code 显示 “Connecting to kernel…” 无限转圈。
实操建议:
- 在 WSL2 里确认
hostname -I输出的 IP 能从 Windows 主机 ping 通;不行就改用localhost+ 端口转发(WSL2 默认支持localhost:xxxx映射) - 远程机器上运行
python -m ipykernel install --user --name myremote --display-name "Python (remote)",确保 kernel 注册成功 - VS Code 连远程时,不要手动填
http://xxx,而应通过Jupyter: Specify Local or Remote Jupyter Server选 “Existing” 并粘贴http://127.0.0.1:8888(前提是已在远程跑jupyter notebook --no-browser --port=8888 --allow-root) - 注意 WSL2 中
jupyter启动后默认绑定127.0.0.1,Windows 无法直连,得加--ip=0.0.0.0并配好防火墙入站规则
真正麻烦的往往不是“没装 jupyter”,而是 VS Code 拿到的 interpreter 和 kernel 注册环境不一致,或者 runtime 文件残留导致握手失败。动手前先看状态栏 kernel 名称和底部日志,比盲目重装高效得多。











