Ask Your Question

Revision history [back]

Instead of .sage files, use .py files. Then everything works pythonically.

The difference between .sage files and .py files is that when Sage reads .py files no preparsing is applied and all imports must be explicit.

For example, if you want to use matrix in such a file, you will have to use

from sage.matrix.constructor import matrix

Note that one can easily know what import statements to include, by using the import_statements function in a Sage session:

sage: import_statements('matrix')
from sage.matrix.constructor import matrix

sage: import_statements('ZZ')
from sage.rings.integer_ring import ZZ

and one can easily know how to replace Sage-specific constructs that take advantage of the Sage preparser, by asking Sage how they are preparsed:

sage: print(preparse('''
....: for i in (1 .. 7):
....:     print(i)
....: '''))

for i in (ellipsis_iter(Integer(1), Ellipsis, Integer(7))):
    print(i)

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