| domain | archive |
|---|---|
| title | Debugging Python Bytecode Cache |
| verification | metadata-normalized |
| source | skill-pipeline |
| status | draft |
| created | 2026-05-09 |
(此 lesson 从 skill debugging-python-bytecode-cache 自动提取,待补全)
(待补充)
Python 代码修改后行为不变——函数返回错误值、返回 None、或表现和旧版本一样。stderr 无语法错误,反复运行结果相同。
-
.pyc缓存文件 — Python import 使用编译后的.pyc字节码,不一定读取.py源文件。如果__pycache__/*.pyc比.py新,Python 优先用缓存。这在以下情况发生:- 文件被修改时 Python 进程正在运行(Python 启动时就预加载了模块)
- 通过 subprocess 调用脚本(gateway 调用 venv python)时,venv 有独立的
__pycache__
-
模块级 vs 嵌套函数缩进 — 批量去缩进代码块时,逐行验证缩进是否正确。用
ast.parse()+inspect.getsource()交叉验证。 -
venv 路径 — 多个 venv 各自有独立的
__pycache__。调试时必须用生产代码实际使用的 Python 解释器。
python3 -c "
import sys; sys.path.insert(0, '/path/to/scripts')
import script_name
import inspect, dis
src = inspect.getsource(script_name.function_name)
print(f'Source lines: {len(src.split(chr(10)))}')
print('Last 5 lines:', src.split('\n')[-5:])
for instr in dis.Bytecode(script_name.function_name):
if 'RETURN' in instr.opname:
print(f'Return at offset: {instr.offset}')
"ls -la /path/to/scripts/__pycache__/*.pyc
stat /path/to/scripts/script.py | grep -E "Modify|Change"rm -f /path/to/scripts/__pycache__/*.pycpython3 -c "import ast; ast.parse(open('/path/to/script.py').read()); print('✅ Syntax OK')"当 hybrid_search 修改后仍然返回空结果,但源码看起来正确时:根因是 stale .pyc(Python 执行的是旧字节码)。执行 rm __pycache__/*.pyc 后恢复正常。
rag_answer.py中_get_tag_filter意外去缩进变成模块级函数hybrid_search在keywords = ...后直接return None__main__也忽略了 CLI 参数(无sys.argv解析)- 修复:重建
hybrid_search函数体 + 添加 CLI 参数解析 - 清理:删除
__pycache__/*.pyc确保新代码被加载
(待补充)