Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Programmatically compiling a Python package into pyc or pyo files

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?

like image 852
Ram Rachum Avatar asked Apr 26 '11 19:04

Ram Rachum


People also ask

How do I compile Python to PYC?

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 ).

What is pyc and pyo?

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).

Is PYC faster than py?

Loading code from serialized . pyc files is faster than parsing the . py file using ANTLR.


2 Answers

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.

like image 105
Emil Sit Avatar answered Sep 27 '22 18:09

Emil Sit


You may want to have a look at the py_compile module. Sadly it won't let you choose between pyo and pyc.

like image 44
Alexander Gessler Avatar answered Sep 27 '22 18:09

Alexander Gessler