VSCode中Jupyter无法识别.venv环境内pip的问题及解决
编辑: 问题已(基本)解决。发现可能存在两个问题因素:其一(本人情况)是最新版Jupyter扩展,切换回2025.8.0版本后恢复正常;其二(zoczkos的情况)是ipykernel版本,9月13日发布的7.0.0版本存在问题,6.30.1版本可正常使用。
编辑2: 将Jupyter VSCode扩展更新至2025.9.1,ipykernel更新至7.0.1后,可确认在VSCode 1.105.0上所有问题已完全解决。
周五离开电脑时,VSCode里打开着多个Python/Jupyter脚本,一切运行正常。回家后发现电脑可能自动重启,或是未正确关机/休眠导致电量耗尽。
重启后,完全无法使用VSCode创建的.venv环境运行任何Jupyter Notebook。
最奇怪的是,pip及所有Jupyter库(notebook、ipykernel、IPython、ipywidgets)均已安装在site-packages目录下,但运行时始终提示:
Running cells with '.venv (Python 3.12.2)' requires the ipykernel and pip package.
点击Install后,又提示:
There is no Pip installer available in the selected environment.
尝试运行VSCode命令Jupyter: Select Interpreter to Start Jupyter Server并选择.venv时,始终收到提示:
Running cells with '.venv (Python 3.12.2)' requires the pip, jupyter and notebook package.
无法通过Jupyter扩展安装任何包,系统显示pip根本未安装。其他将Jupyter关联到.venv的方式(如Python: Select Interpreter)也均失败。
Jupyter已检测到.venv中的内核,但就是无法识别已安装的包。复制Jupyter输出的安装命令在.venv终端中运行,执行无问题,但Jupyter仍无法识别。
Jupyter输出的命令信息:
Running cells with '.venv (Python 3.12.2)' requires the ipykernel package. Install 'ipykernel' into the Python environment. Command: "d:/path/.venv/Scripts/python.exe" -m pip install ipykernel -U --force-reinstall
- 选中
.venv作为解释器,无效果。 - 卸载VSCode并删除
.vscode文件夹后重装,问题仍存在。 - 重装Python 3.12.2,问题依旧。
- 用
requirements.txt重新创建.venv,无效。 - 修改VSCode设置(仍失败):
"python.defaultInterpreterPath": ".\\.venv\\Scripts\\python.exe", "jupyter.allowUnauthorizedRemoteConnection": true, "python.terminal.activateEnvironment": true, "python.venvPath": ".\\.venv\\Scripts\\python.exe", "python.venvFolders": ["venv", ".venv"] - 在
.venv终端运行py -m ipykernel install --user --name=.venv,在C:\Users\user\AppData\Roaming\jupyter\kernels\.venv创建了虚拟环境,但无济于事。
Jupyter内核列表(执行jupyter kernelspec list)
Available kernels: python3 d:\path\.venv\share\jupyter\kernels\python3 .venv C:\Users\user\AppData\Roaming\jupyter\kernels\.venv
Jupyter调试输出
>jupyter --version Selected Jupyter core packages... IPython : 9.6.0 ipykernel : 6.30.1 ipywidgets : 8.1.7 jupyter_client : 8.6.3 jupyter_core : 5.8.1 jupyter_server : 2.17.0 jupyterlab : 4.4.9 nbclient : 0.10.2 nbconvert : 7.16.6 nbformat : 5.10.4 notebook : 7.4.7 qtconsole : not installed traitlets : 5.14.3 >jupyter --paths --debug JUPYTER_PLATFORM_DIRS is set to a false value, or is not set, so we use hardcoded legacy paths for platform-specific directories JUPYTER_PREFER_ENV_PATH is set to a true value, or JUPYTER_PREFER_ENV_PATH is not set and we detected a virtual environment, making the environment-level path preferred over the user-level path for data and config JUPYTER_NO_CONFIG is not set, so we use the full path list for config JUPYTER_CONFIG_PATH is not set, so we do not prepend anything to the config paths JUPYTER_CONFIG_DIR is not set, so we use the default user-level config directory Python's site.ENABLE_USER_SITE is not True, so we do not add the Python site user directory 'C:\Users\user\AppData\Roaming\Python' JUPYTER_PATH is not set, so we do not prepend anything to the data paths JUPYTER_DATA_DIR is not set, so we use the default user-level data directory JUPYTER_RUNTIME_DIR is not set, so we use the default runtime directory config: d:\path\.venv\etc\jupyter C:\Users\user\.jupyter data: d:\path\.venv\share\jupyter C:\Users\user\AppData\Roaming\jupyter runtime: C:\Users\user\AppData\Roaming\jupyter\runtime
- Python版本:3.12.2
- VSCode版本:1.105.0
- Jupyter扩展版本:2025.9.0
- Windows版本:11 24H2 26100.6725
内容的提问来源于stack exchange,提问作者Soren V. Raben

