Ask Your Question

Revision history [back]

The solution is to use a filename.py file.

For the sage specific syntax, find the corresponding Python code using preparse.

Find the sage specific functions to import using import_statements.

For example, suppose that your sage file has

R.<x> = PolynomialRing(QQ)

which is not correct Python syntax. Check how to turn that into correct Python.

sage: preparse('R.<x> = PolynomialRing(QQ)')
"R = PolynomialRing(QQ, names=('x',)); (x,) = R._first_ngens(1)"

so in filename.py you would write

R = PolynomialRing(QQ, names=('x',))
(x,) = R._first_ngens(1)

instead of

R.<x> = PolynomialRing(QQ)

You would also need the import statements, which you find with

sage: import_statements('PolynomialRing')
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
sage: import_statements('QQ')
...
from sage.rings.rational_field import QQ

So your python file would really have

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.rings.rational_field import QQ

R = PolynomialRing(QQ, names=('x',))
(x,) = R._first_ngens(1)