I am looking for a way to pass miscellaneous options using runpy or other tools.
In particular, I would like to get the output of an optimized python script in another non-optimized python script.
python -O tobeoptimized.py
I have tried using subprocess but I can't extract the object that I need as I do in runpy.
from subprocess import PIPE, run
command = ['python','-O','tobeoptimized.py']
result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True)
It's doable.
The
compilebuilt-in function takes optimize parameter. Its values are 0, 1 and 2. If it's0which is like without-O,1is-Oand2is-OOon the command line.To make
runpyrun module / path optimized it must be patched. Before doing that I'll define two functions for illustration.This is a test module. If it's run without optimization it won't print.
app.py
These two functions don't do all the details as
runpydoes. So won't function in full.This one is to patch a function in
runpywhichrunpy.run_moduleuses to get code object of the module to run. The patch provides optimized code object.UPDATE
runpy.run_pathcan be run with optimization turned on.