Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The problem is: there is no such thing as a "cython script". Cython builds python extension modules, which can contain code that runs when the extension module is loaded (with "import").

There is the sage command "cython", however, which builds the module and immediately loads it, and imports its bindings into the toplevel namespace. That makes it seem you can use scripts.

A "standalone" script could be written using something like

#!/usr/local/bin/sage --python
from sage.all import *
from sage.repl.user_globals import set_globals
set_globals(globals())
cython("""
def cyprint(s):
    print(s)
""")
cyprint("Hello world")

If you put that in a file and mark it executable, you should be able to execute the script by typing in its name (together with a path, depending on your configuration). You can't do this directly with a "sage" script since it would want to preparse things. The "set_globals" thing is a peculiarity that the "cython" command needs. It may be possible to streamline this use further.

Of course this is a very inefficient way to go about things: you'll be recompiling this module every time you invoke the script. For any serious work you should just build a cython module and make sure it's in a place where you can import that module in the script in which you want to use it.