Skip to content

Instantly share code, notes, and snippets.

@ishikawa
Created September 21, 2008 04:01
Show Gist options
  • Select an option

  • Save ishikawa/11835 to your computer and use it in GitHub Desktop.

Select an option

Save ishikawa/11835 to your computer and use it in GitHub Desktop.
Using os.spawnv to run and compile files with or without optimization
def compile_python_source(filepath, optimization=False):
"""
Compile a source file to byte-code and write out
the byte-code cache file. The source code is loaded
from the file name *filepath*. The byte-code is cached
in the normal manner (*filepath* + 'c' or 'o' if
optimization is enabled).
"""
from os import spawnv
from sys import executable, platform
args = [executable]
if optimization:
args.append('-O')
args += ['-m', 'py_compile']
if isinstance(filepath, (tuple, list)):
args.extend(filepath)
else:
args.append(filepath)
if platform == "win32":
args = ['"%s"' % arg for arg in args]
return spawnv(os.P_WAIT, executable, args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment