Ask Your Question

Revision history [back]

Import, load or run your code and fix each problem as it comes.

Regarding missing imports, use the import_statements function.

sage: import_statements(var)
from sage.calculus.var import var
sage: import_statements(exists)
from sage.misc.misc import exists
sage: import_statements(forall)
from sage.misc.misc import forall
sage: import_statements(DiGraph)
from sage.graphs.digraph import DiGraph

Import, load or run your code and fix each problem as it comes.

Regarding missing imports, use the import_statements function.

sage: import_statements(var)
from sage.calculus.var import var
sage: import_statements(exists)
from sage.misc.misc import exists
sage: import_statements(forall)
from sage.misc.misc import forall
sage: import_statements(DiGraph)
from sage.graphs.digraph import DiGraph

This will make the code run in Python with access to the Sage library.

If you want to remove access to the Sage library, it's different.

Look into all and any to replace use of forall and exists.

Regarding var, SymPy certainly has similar functionality, and would be lighter than Sage.

Regarding graphs, see what graph libraries have the functionality you need.

To sum up the question: we have a file myfile.sage and the question is to turn it into a myfile.py file.

Automatic

The automatic way is to run in the terminal:

$ sage --preparse myfile.sage

which will produce a file myfile.sage.py that is a preparsed version of the original file.

In terms of imports, it adds the following import:

from sage.all_cmdline import *   # import sage library

Manual

Import, load or load, run your code and fix each problem as it comes.

Regarding missing imports, use the import_statements function.

sage: import_statements(var)
from sage.calculus.var import var
sage: import_statements(exists)
from sage.misc.misc import exists
sage: import_statements(forall)
from sage.misc.misc import forall
sage: import_statements(DiGraph)
from sage.graphs.digraph import DiGraph

This will give minimal imports that make the code run in Python with (with access to the Sage library.library).

If you want to remove access to the Sage library, it's different.

Look into all and any to replace use of forall and exists.

Regarding var, SymPy certainly has similar functionality, and would be lighter than Sage.

Regarding graphs, see what graph libraries have the functionality you need.