Differences between revisions 9 and 10
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
Typically Cython is used to create extension modules for use from Python programs. One may, however, one want to write a standalone program in Cython. This is done via embedding the Python interpreter with the {{{--embed}}} option.  Typically Cython is used to create extension modules for use from Python programs. It is, however, possible to write a standalone programs in Cython. This is done via embedding the Python interpreter with the {{{--embed}}} option.
Line 7: Line 7:
 * See also [[http://thread.gmane.org/gmane.comp.python.cython.devel/669|this]] email thread, and also more [[http://thread.gmane.org/gmane.comp.python.cython.devel/5512/focus=5517|embedding discussion here]] and a [[http://hg.cython.org/cython/file/8bff3332e34f/bin/cython_freeze|script]] to "freeze" Cython modules (like Python freeze).   * See also [[http://thread.gmane.org/gmane.comp.python.cython.devel/669|this]] email thread, and also more [[http://thread.gmane.org/gmane.comp.python.cython.devel/5512/focus=5517|embedding discussion here]] and a [[https://github.com/cython/cython/blob/master/bin/cython_freeze|script]] to "freeze" Cython modules (like Python freeze).
Line 9: Line 9:
 * This page is derived from [[http://www.freenet.org.nz/python/embeddingpyrex/|David McNab's]] idea for embedding Pyrex. The concepts there have been implemented in the {{{--embed}}} flag for Python.  * This page is derived from [[http://www.freenet.org.nz/python/embeddingpyrex/|David McNab's]] idea for embedding Pyrex. The concepts there have been implemented in the {{{--embed}}} flag for Cython.
Line 13: Line 13:
This has been implemented--simply run cython with the --embed option and a .c file with a main method is created and the module's name will be set to {{{__main__}}}. Compile the .c file linking with the Python library. Running the resulting executable execute the module. This has been implemented--simply run cython with the --embed option and a .c file with a main method is created which sets its module name to {{{__main__}}} (just as for normal executable Python scripts). Compile the .c file and link it against the Python library. Starting the resulting binary will then execute the module code.
Line 15: Line 15:
A demo is up at [[https://github.com/cython/cython/tree/master/Demos/embed]]. A complete demo can be found at at [[https://github.com/cython/cython/tree/master/Demos/embed]].

Embedding Cython

Typically Cython is used to create extension modules for use from Python programs. It is, however, possible to write a standalone programs in Cython. This is done via embedding the Python interpreter with the --embed option.

Background

  • See also this email thread, and also more embedding discussion here and a script to "freeze" Cython modules (like Python freeze).

  • This page is derived from David McNab's idea for embedding Pyrex. The concepts there have been implemented in the --embed flag for Cython.

Status

This has been implemented--simply run cython with the --embed option and a .c file with a main method is created which sets its module name to __main__ (just as for normal executable Python scripts). Compile the .c file and link it against the Python library. Starting the resulting binary will then execute the module code.

A complete demo can be found at at https://github.com/cython/cython/tree/master/Demos/embed.

EmbeddingCython (last edited 2013-02-16 21:06:57 by StefanBehnel)