Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling .py files to .pyo without resorting to a wrapper script

Tags:

python

i am looking into compiling quite a big set of python modules and packages to pyo. I know this is possible by either setting the PYTHONOPTIMIZE environment variable or by specifying -O on launch. I'd like to enforce pyo instead of pyc to yield the smallest footprint possible. In order to do that in my deploy module i have to create a wrapper script that launches the actual script with the -O option, because the environment variable needs to be specified prior to starting the interpreter.

Is there any way around this and enforce pyo creation programmatically?

Kind regards, Thorsten

like image 709
instinct-vfx Avatar asked Jul 13 '12 14:07

instinct-vfx


People also ask

How do I compile a .PY file?

You can also automatically compile all Python files using the compileall module. You can do it from the shell prompt by running compileall.py and providing the path of the directory containing the Python files to compile: monty@python:~/python$ python -m compileall .

What is Py_compile?

The py_compile module provides a function to generate a byte-code file from a source file, and another function used when the module source file is invoked as a script.


2 Answers

To compile all modules beforehand, run the following command:

python -O -m compileall /path/to/your/files

The python compileall module takes care of the compilation, the -O switch makes it output .pyo files.

However, you cannot force Python to use these unless the -O switch is given for every run or the PYTHONOPTIMIZE environment var is set.

Note that all the -O flag does is disable the assert statement and set the __debug__ flag to False (and Python will optimise out the tests). Specify -OO and docstrings are dropped. These do not make for much speed difference or space savings, unless you used excessive docstring sizes or very slow debug code.

See: What does Python optimization (-O or PYTHONOPTIMIZE) do?

like image 102
Martijn Pieters Avatar answered Sep 25 '22 03:09

Martijn Pieters


Sadly from version 3.5 the compile optimization only makes .pyc files, not .pyo

Changed in version 3.5: The legacy parameter only writes out .pyc files, not .pyo files no matter what the value of optimize is.

From https://docs.python.org/3.6/library/compileall.html

like image 39
Stefano Goldoni Avatar answered Sep 25 '22 03:09

Stefano Goldoni