This is for my test suite.
I have an automatically-generated Python package in a temporary folder. It's all .py
files. I want to programmatically compile these into (a) .pyc
and (b) .pyo
files. (One test will do .pyc
, another will do .pyo
.) This should be done with the active interpreter, of course. I do not want to import the modules, just compile.
How can I do this?
Method 1: using py_compile module Import py_compile in your python code. There is a function called compile() . Pass the Python file name that you want to compile as an argument. It will convert Python script into the bytecode (file with the file extension pyc ).
A PYC file is the bytecode file generated and read from when no optimization level is specified at interpreter startup (i.e., -O is not specified). A PYO file represents the bytecode file that is read/written when any optimization level is specified (i.e., when -O or -OO is specified).
Loading code from serialized . pyc files is faster than parsing the . py file using ANTLR.
In your Python lib directory, there will be a script called compileall.py
(e.g., /usr/lib/python2.6/compileall.py
).
In your code, spawn (e.g., by using os.spawnl
) an invocation of compileall.py
pointed at the directory containing your generated code. If you invoke it using python -O
it will generate .pyo
files; if you invoke it using python
it will generate .pyc
file.
The trick, I suppose, would be to call with the right version of the Python interpreter.
compileall.py
uses py_compile
under the hood.
You may want to have a look at the py_compile
module. Sadly it won't let you choose between pyo
and pyc
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With