Compiling Python Source into Bytecode
Normally, when a python module is imported, python automatically generate a .pyc file containing the compiled byte code. The next time the same program is run, the compilation step can thus be skipped.
However, in order to manually pre-compile the python modules even before running them, here are two ways of doing it:
Use a python script
To compile a single file / module, use the
py_compile
module.import py_compile if __name__ == "__main__": py_compile.compile('abc.py')
To compile all files in a directory, use the
compileall
module.import compileall if __name__ == "__main__": compileall.compile_dir('Lib/')
Execute on a shell / command line.
The compileall module can also be invoked as a script. On the command line, type:
python -m compileall Lib\
Read other posts
comments powered by Disqus