4 Compile a Python script into an executable that embeds CPython and run it.
5 Requires CPython to be built as a shared library ('libpythonX.Y').
9 python cythonrun somefile.py [ARGS]
17 from distutils import sysconfig
19 INCDIR = sysconfig.get_python_inc()
20 LIBDIR1 = sysconfig.get_config_var('LIBDIR')
21 LIBDIR2 = sysconfig.get_config_var('LIBPL')
22 PYLIB = sysconfig.get_config_var('LIBRARY')[3:-2]
24 CC = sysconfig.get_config_var('CC')
25 CFLAGS = sysconfig.get_config_var('CFLAGS') + ' ' + os.environ.get('CFLAGS', '')
26 LINKCC = sysconfig.get_config_var('LINKCC')
27 LINKFORSHARED = sysconfig.get_config_var('LINKFORSHARED')
28 LIBS = sysconfig.get_config_var('LIBS')
29 SYSLIBS = sysconfig.get_config_var('SYSLIBS')
32 def _debug(msg, *args):
35 sys.stderr.write(msg + '\n')
40 _debug('INCDIR: %s', INCDIR)
41 _debug('LIBDIR1: %s', LIBDIR1)
42 _debug('LIBDIR2: %s', LIBDIR2)
43 _debug('PYLIB: %s', PYLIB)
45 def runcmd(cmd, shell=True):
52 returncode = subprocess.call(cmd, shell=shell)
57 runcmd([LINKCC, '-o', basename, basename+'.o', '-L'+LIBDIR1, '-L'+LIBDIR2, '-l'+PYLIB]
58 + LIBS.split() + SYSLIBS.split() + LINKFORSHARED.split())
60 def ccompile(basename):
61 runcmd([CC, '-c', '-o', basename+'.o', basename+'.c', '-I' + INCDIR] + CFLAGS.split())
63 def cycompile(input_file, options=()):
64 from Cython.Compiler import Version, CmdLine, Main
65 options, sources = CmdLine.parse_command_line(list(options or ()) + ['--embed', input_file])
66 _debug('Using Cython %s to compile %s', Version.version, input_file)
67 result = Main.compile(sources, options)
68 if result.num_errors > 0:
71 def exec_file(basename, args=()):
72 runcmd([os.path.abspath(basename)] + list(args), shell=False)
76 for i, arg in enumerate(args):
77 if arg.startswith('-'):
84 raise ValueError('no input file provided')
85 basename = os.path.splitext(input_file)[0]
86 cycompile(input_file, cy_args)
89 exec_file(basename, args)
91 if __name__ == '__main__':