已激活 venv,VS Code Debugger 仍找不到 streamlit
结论
在终端中激活 venv,只会影响从该终端启动的进程,无法自动指定 VS Code Debugger 使用的 Python。
出现这个报错,通常是因为 Debugger 启动的 Python 与执行 streamlit run app.py 的 Python 不同。调试配置直接运行 app.py,没有通过 Streamlit 的模块入口启动应用,也会导致类似问题。
先分别确认两个进程的 sys.executable,再把 Debugger 切换到同一个解释器,并按照 python -m streamlit run app.py 的方式启动应用。
为什么 pip list 不能证明 Debugger 使用了正确环境
下面三条命令可能分别指向不同的 Python 环境:
pip list
python app.py
streamlit run app.py
例如,streamlit 命令可能来自某个虚拟环境,pip 或 VS Code Debugger 却使用了另一个解释器。即使 VS Code 状态栏显示的是正确环境,.vscode/launch.json、工作区设置或其他运行扩展也可能覆盖这一选择。
检查依赖时,最好通过目标 Python 调用 pip:
python -m pip show streamlit
python -c "import sys; print(sys.executable)"
python -c "import streamlit; print(streamlit.__file__)"
如果最后一条命令可以正常执行,当前这个 python 就能导入 Streamlit。
还可以查看终端中的命令来自哪里:
which python
which pip
which streamlit
Windows 使用:
where python
where pip
where streamlit
检查 Debugger 实际使用的 Python
创建一个临时诊断文件,例如 check_env.py:
import importlib.util
import sys
print("Python executable:", sys.executable)
print("Python version:", sys.version)
print("streamlit spec:", importlib.util.find_spec("streamlit"))
print("sys.path:")
print("\n".join(sys.path))
通过 VS Code Debugger 运行该文件,再将输出的 sys.executable 与终端中的结果进行比较:
python -c "import sys; print(sys.executable)"
两个路径应指向同一个虚拟环境,例如:
/path/to/project/.venv/bin/python
Windows 中通常类似:
C:\path\to\project\.venv\Scripts\python.exe
如果 Debugger 输出的是系统 Python、Conda 环境或其他项目的虚拟环境,说明两边使用的解释器不一致。
推荐的 launch.json
Streamlit 应用需要通过 Streamlit 的入口启动,日常运行命令是:
python -m streamlit run app.py
对应的 VS Code 调试配置可以写成:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Streamlit",
"type": "debugpy",
"request": "launch",
"module": "streamlit",
"args": [
"run",
"${workspaceFolder}/app.py"
],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"justMyCode": true
}
]
}
其中的 "module": "streamlit" 相当于执行:
python -m streamlit run app.py
部分较旧的 Python 扩展配置使用:
"type": "python"
如果当前环境不识别 "debugpy",可以将其改为 "python",其他字段无需调整。
同时检查配置中是否有指向其他环境的 "python" 字段。下面的配置会覆盖状态栏中选择的解释器:
{
"python": "/usr/bin/python3"
}
如果不需要固定解释器路径,可以删除该字段,让 Python 扩展使用当前工作区选中的解释器。
重新绑定工作区解释器
在 VS Code 中按以下步骤操作:
- 打开命令面板。
- 运行
Python: Select Interpreter。 - 选择项目虚拟环境中的 Python。
- 运行
Developer: Reload Window。 - 再次选择
Debug Streamlit配置启动调试。
如果列表中没有目标解释器,可以手动输入路径。
macOS 或 Linux:
${workspaceFolder}/.venv/bin/python
Windows:
${workspaceFolder}\.venv\Scripts\python.exe
也可以在工作区设置中指定默认路径:
{
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python"
}
Windows 项目可改为:
{
"python.defaultInterpreterPath": "${workspaceFolder}\\.venv\\Scripts\\python.exe"
}
修改后需要重新选择解释器或重新加载窗口。工作区如果已经缓存了解释器,可能不会立即采用新的默认值。
使用解释器绝对路径做最终验证
如果仍无法确定两边的环境是否一致,可以跳过终端的激活状态,直接调用虚拟环境中的 Python。
macOS 或 Linux:
./.venv/bin/python -m pip show streamlit
./.venv/bin/python -m streamlit run app.py
Windows:
.\.venv\Scripts\python.exe -m pip show streamlit
.\.venv\Scripts\python.exe -m streamlit run app.py
如果命令提示没有安装 Streamlit,请使用同一个解释器安装依赖:
./.venv/bin/python -m pip install -r requirements.txt
Windows:
.\.venv\Scripts\python.exe -m pip install -r requirements.txt
还需要检查的配置
如果 Debugger 仍然使用了错误的解释器,请检查以下项目:
.vscode/launch.json中是否存在"python"字段或旧的解释器绝对路径。.vscode/settings.json中是否保留了其他虚拟环境的路径。- 当前是否打开了多根工作区,以及解释器是否选在了错误的工作区目录下。
- 启动按钮是否来自 Code Runner 等其他扩展,而非 VS Code 的
Run and Debug。 launch.json是否使用了"program": "${file}"。调试 Streamlit 时应优先使用"module": "streamlit"。- 安装依赖后,Debugger 是否仍在使用此前启动的旧终端或旧调试会话。
envFile 只能注入环境变量,无法激活虚拟环境,因此只设置 .env 通常不能解决这个导入错误。
判断环境是否一致,应以实际进程的 sys.executable 为准,不能只看状态栏、提示符中的环境名称或单独执行的 pip list。确认两边的解释器路径相同,并且 python -m streamlit run app.py 可以运行后,上面的调试配置才会使用同一套依赖。
备注:内容仅供参考。
