Skip to main content
 首页 » 编程设计

python之编译 *.py 文件

2024年10月25日10dudu

我试图编译 python 源文件但没有成功。根据documentation ,compileall.compile_dir 函数有“ddir”参数,(我猜)它指定了 .pyc 文件的目标文件夹。我尝试用这个脚本编译它:

import compileall 
 
compileall.compile_dir(".", force=1, ddir=".") 

但它不起作用。我看到了终端的输出(编译、列表等),但没有生成 pyc 文件。任何人都可以帮助我了解 pyc 文件的存储位置以及如何更改该默认行为?

谢谢

请您参考如下方法:

检查 __pycache__ 目录。从 Python 3.2 开始,编译后的文件收集在 __pycache__ 目录中。

参见 PEP 3147: PYC Repository Directories - What’s New In Python 3.2 .

顺便说一句,ddir 不是目标目录。根据the documentation :

If ddir is given, it is prepended to the path to each file being compiled for use in compilation time tracebacks, and is also compiled in to the byte-code file, where it will be used in tracebacks and other messages in cases where the source file does not exist at the time the byte-code file is executed.