Wednesday, April 25, 2012

Compile Python Code

hohoho,.......
This should have been done a long time to execute if the python in the compiled file, to make the file more quickly if the execution. Python source code is automatically compiled into Python byte code by the CPython interpreter. Compiled code is usually stored in PYC (or PYO) files, and is regenerated when the source is updated, or when otherwise necessary.
Python automatically compiles Python source code when you import a module, so the easiest way to create a PYC file is to import it. If you have a module sampleModule.py, just do:
>>> import sampleModule
to create a sampleModule.pyc file in the same directory.
To do this programmatically, and without executing the code, you can use the py_compile module:
>>>import py_compile

>>>py_compile.compile("fileModule.py")

There’s also a compileall module which can be used to compile all modules in an entire directory tree.
>>>import compileall

>>>compileall.compile_dir("folderDestination", force=1)

Good luck, :)

No comments:

Post a Comment