Cython编译main函数


Because Cython works closely with the Python/C API and runtime environment, Cy‐ thon source code is nearly always compiled into a dynamic extension module and im‐ ported by Python code. But the cython compiler does have an option to embed the Python interpreter inside a main function. This makes it possible to use Cython to create a standalone executable that can be run directly from the command line.

An Example

snippet.python
import sys 
from math import pi,e
import commands
 
def main():
    print "hello world"
    print "pi**e == {:.2f}".format(pi**e)
    exitstatus, outtext = commands.getstatusoutput("which python")
    print outtext
    return exitstatus
 
if __name__ == "__main__":
    if main() == 0:
        exit(0)
    else:
        exit(-1)

Compile python code like C:

snippet.bash
# compile
cython --embed test.py 
gcc $(python-config --cflags) $(python-config --ldflags) ./test.c -o test
 
# execute
./test
  • 公共/tech/cython编译main函数.txt
  • 最后更改: 7年前
  • 由 rongzhengqin