Reputation: 1368
Cython can sometimes remarkably decrease computational time by converting the code and running it in C. Is there a way to use Cython through a normal script by using some import-- function?
I downloaded Python (x y) and am not even sure what the 'best' way is to use Python. iPython notebook or Spyder etc ... ?
What are my other options?
Upvotes: 3
Views: 1542
Reputation: 180391
You can write your cython code in your .pyx file.
Then use pyximport to compile it by adding something like:
`import pyximport;
pyximport.install()`
from foo import foo # finds foo.pyx automatically
It detects changes in a cython file and recompiles if necessary or else loads the cached module.
A simple example running from ipython in the same directory as my fib.pyx
:
My fib.pyx
looks like:
def cython_fib(int n):
cdef long i,a, b
a,b = 0,1
for i in xrange(n):
a,b = b,a + b
return a
Then I import pyximport etc..
In [1]: import pyximport
In [2]: pyximport.install()
Out[2]: (None, <pyximport.pyximport.PyxImporter at 0x7f0139916e10>)
In [3]: from fib import cython_fib # import like a normal python import
In [4]: cython_fib(20)
Out[4]: 6765
Upvotes: 4