Compile
Optimize
-O Turn on basic optimizations. This changes the filename extension for compiled (bytecode) files from .pyc to .pyo. Given twice, causes docstrings to be discarded.
# $ python -h
# -m mod : run library module as a script (terminates option list)
# -O : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x
# -OO : remove doc-strings in addition to the -O optimizations
Default
In [1]: import sys In [2]: sys.flags.optimize Out[2]: 0 In [3]: __debug__ Out[3]: True
Pyc
Command-line
python -m py_compile $filename
Script
import py_compile py_compile.compile('$filename') # $filename is the file name of python source code.
Pyo
Command-line
python -O -m py_compile $filename python -OO -m py_compile $filename
Compile Dir
import compileall
compileall.compile_dir('$dir')
Alias
###########
# Pyc & Pyo
###########
# [32.10. py_compile — Compile Python source files](https://docs.python.org/2/library/py_compile.html)
alias pyc='python -m py_compile'
alias pyo='python -O -m py_compile'
alias pyo1='python -O -m py_compile'
alias pyo2='python -OO -m py_compile'
# [32.11. compileall — Byte-compile Python libraries](https://docs.python.org/2/library/compileall.html)
alias pycdir='python -m compileall'
alias pyodir='python -O -m compileall'
alias pyo1dir='python -O -m compileall'
alias pyo2dir='python -OO -m compileall'
References
[1] Docs@Python, 32.10. py_compile — Compile Python source files
[2] Docs@Python, 32.11. compileall — Byte-compile Python libraries
[3] Docs@Python, What’s New In Python 3.5 - PEP 488: Elimination of PYO files
[4] Docs@Python, 1. Command line and environment
[5] u0b34a0f6ae@StackOverflow, What is the use of Python's basic optimizations mode? (python -O)
[6] zaharpopov@StackOverflow, python optimized mode
[7] max@StackOverflow, What does Python optimization (-O or PYTHONOPTIMIZE) do?