Cython ipython magic with compile time environment variables

The %%cython very convenient for creating cython functions without creating and using a package. The command has several options, but I could not find a way to specify compile-time temporary variables.

I want the equivalent:

 from Cython.Distutils.extension import Extension ext = Extension(... cython_compile_time_env={'MYVAR': 10}, ...) 

for the %%cython .

I already tried:

 %%cython -cython_compile_time_env={'MYVAR':10} IF MYVAR: def func(): return 1 ELSE: def func(): return 2 

But this throws an exception:

 Error compiling Cython file: ------------------------------------------------------------ ... IF MYVAR: ^ ------------------------------------------------------------ ...\.ipython\cython\_cython_magic_28df41ea67fec254f0be4fc74f7a6a54.pyx:2:8: Compile-time name 'MYVAR' not defined 

and

 %%cython --cython_compile_time_env={'MYVAR':10} IF MYVAR: def func(): return 1 ELSE: def func(): return 2 

throws

UsageError: unrecognized arguments: --cython_compile_time_env = {'MYVAR': 10}

+5
source share

Source: https://habr.com/ru/post/1262478/


All Articles