python -m compileall src/
把
.py源码提前编译成.pyc字节码
平时你 import xxx 时,Python 也会做这件事,只是compileall 是"提前、批量做"。
-m compileall
-
-m:以模块方式运行 -
compileall:Python 自带模块(不是第三方)
作用:
-
遍历目录
-
编译所有
.py -
生成
.pyc
3️⃣ src/
-
要编译的目录
-
会 递归 处理所有子目录
执行后会生成什么?
假设你原来结构是:
src/ main.py utils.py core/ algo.py
执行后:
src/ main.py utils.py core/ algo.py __pycache__/ main.cpython-39.pyc utils.cpython-39.pyc core/__pycache__/ algo.cpython-39.pyc
📌 重点:
-
.py还在 -
.pyc放在__pycache__