Python 文件编译为字节码的方法

一般情况下 python 不需要手动编译字节码。但是如果不想直接 release 源代码给其他人,将文件编译成字节码,可以实现一定程度的信息隐藏。

1) 使用模块 py_compile 编译一个单文件

import py_compile
py_compile.compile('./demo.py')

2) 使用模块 compileall 递归的编译一个文件包

import compileall
compileall.compile_dir('./test')

3) 使用模块 compileall 编译一个单文件

import compileall
compileall.compile_file('./demo.py')

更多应用请详细阅读这两个模块的帮助文件。

完。

原文地址:https://www.cnblogs.com/gaowengang/p/8495288.html