Python项目VSCode卡顿主因是venv等目录被默认纳入文件监视,必须同步配置search.exclude、files.watcherExclude和files.exclude三者,缺一不可;改后须执行workbench.action.reloadWindow重载窗口方可生效。

为什么Python项目在VSCode里打开就卡CPU
根本不是Python语言服务慢,而是VSCode默认把venv、.mypy_cache、__pycache__这些目录全塞进文件监视和索引队列。一个中等规模Django项目光venv就能触发上万次fs.watch事件,主线程直接被拖垮。
必须同步配齐的三个排除配置
search.exclude、files.watcherExclude、files.exclude三者作用不同,漏掉任意一个都会失效:
-
search.exclude只跳过搜索时的内容读取,不影响文件监听 -
files.watcherExclude阻止系统注册监听路径,从源头掐断I/O风暴 -
files.exclude让资源管理器里根本看不到那些目录,减少UI渲染负担
真实有效的.vscode/settings.json片段(直接复制进项目根目录):
{
"search.exclude": {
"**/venv/**": true,
"**/.venv/**": true,
"**/env/**": true,
"**/__pycache__/**": true,
"**/.mypy_cache/**": true,
"**/*.pyc": true,
"**/*.pyo": true
},
"files.watcherExclude": {
"**/venv/**": true,
"**/.venv/**": true,
"**/env/**": true,
"**/__pycache__/**": true,
"**/.mypy_cache/**": true,
"**/pip-wheel-metadata/**": true
},
"files.exclude": {
"**/venv/**": true,
"**/.venv/**": true,
"**/env/**": true,
"**/__pycache__/**": true,
"**/.mypy_cache/**": true,
"**/*.pyc": true
}
}
Python特有坑:.gitignore里的路径不自动生效
search.useIgnoreFiles默认为true,但VSCode的Python语言服务器(Pylance)会绕过它单独扫描.gitignore里没写的缓存目录。比如.pytest_cache或.ruff_cache,即使你写了.gitignore,也得手动加进files.watcherExclude。
立即学习“Python免费学习笔记(深入)”;
- 检查当前项目是否生成了
.pytest_cache:ls -d .pytest_cache - 如果存在,立刻补进
files.watcherExclude:"**/.pytest_cache/**": true - 同理处理
.ruff_cache、.blackd等工具缓存目录
改完配置后必须重载窗口,不是重启VSCode
Ctrl+P → 输入workbench.action.reloadWindow执行重载——这是唯一能让files.watcherExclude生效的操作。单纯关闭再打开窗口,旧的监听句柄还在跑。
如果重载后仍卡顿,手动删掉工作区下的.vscode/.search目录(若存在),强制重建索引。注意:.vscode是隐藏目录,macOS/Linux下用ls -a可见。
最后确认Workspace Trust状态:右下角锁图标要是“Trusted”,否则所有exclude设置会被忽略。



















