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