Ask Your Question
2

How to use cython functions from other cython cells in notbook?

asked 2013-01-11 18:02:42 +0200

Steven gravatar image

updated 2013-01-11 18:06:00 +0200

Hi, I'm currently using sage with the notebook interface. How can I define a function in a cython cell and use it in another cython cell?

I understand I need to import the first cython module but the name changes after every evaluation.

Also being able to do this directly from notebook (without writing a separate cython file) and from separate cells would be handy.

For example this works fine:

cell1:

%cython
def test(int n):
    return 2*n

cell 2:

def test2(m):
    return test(m)

cell 3:

test2(3)
5

but this does not:

cell 4:

%cython
def test3(int m):
    return test(m)
pyx:7:15: undeclared name not builtin: test

Thank you :)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2013-01-12 17:00:00 +0200

This works for me:

%cython
from __main__ import test

def test3(int m):
    return test(m)

The function name test is making things more confusing in this case. Python has a module named test, so if you just use import test, the module masks the previously defined function.

edit flag offensive delete link more

Comments

1

Renaming the function to `test00001` and `import test00001` in the other cell also doesn't work. It gets an error `ImportError: No module named test00001`. But your method works.

ppurka gravatar imageppurka ( 2013-01-12 20:12:17 +0200 )edit

Would be nice to explain such a basic thing in the documentation regarding cython in Sage.

anatematic gravatar imageanatematic ( 2013-01-14 09:48:52 +0200 )edit

Thank you. That did the trick!

Steven gravatar imageSteven ( 2013-01-14 12:07:27 +0200 )edit
1

@anatematic I have opened [#13952](http://trac.sagemath.org/sage_trac/ticket/13952)

ppurka gravatar imageppurka ( 2013-01-14 12:53:57 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2013-01-11 18:02:42 +0200

Seen: 3,371 times

Last updated: Jan 12 '13