undefined
编译 Python 文件为字节码,快速检查语法错误而不执行代码
语法
python -m py_compile <file> python -m compileall <dir>
示例
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
技巧
- py_compile 只检查语法,不执行代码也不检查导入
- compileall 适合在 CI 中批量检查所有文件
- 编译生成的 .pyc 文件在 __pycache__ 目录中
- 更全面的检查用 ruff check 或 flake8(包含逻辑错误检测)
- 语法检查比运行代码快得多,适合 pre-commit hook