Ask Your Question

run Python file from command line in Sage

asked 2013-05-31 02:25:44 -0500

updated 2013-05-31 02:31:56 -0500

I'm trying to run some Python file with Sage. I can import the module just fine within the interactive Sage IPython shell.

However, this very simple test fails:


where contains the single line:

from sage.matrix.matrix2 import Matrix

It fails with:

Traceback (most recent call last):
  File "", line 1, in <module>
    from sage.matrix.matrix2 import Matrix
  File "matrix2.pyx", line 1, in init sage.matrix.matrix2 (sage/matrix/matrix2.c:71259)
  File "matrix1.pyx", line 1, in init sage.matrix.matrix1 (sage/matrix/matrix1.c:13931)
  File "matrix0.pyx", line 32, in init sage.matrix.matrix0 (sage/matrix/matrix0.c:29567)
  File "/Applications/sage-5.9/local/lib/python2.7/site-packages/sage/modules/", line 163, in <module>
    import free_module_element
  File "integer.pxd", line 9, in init sage.modules.free_module_element (sage/modules/free_module_element.c:30936)
  File "integer.pyx", line 178, in init sage.rings.integer (sage/rings/integer.c:38404)
  File "/Applications/sage-5.9/local/lib/python2.7/site-packages/sage/rings/", line 203, in <module>
    import sage.rings.rational
  File "fast_arith.pxd", line 5, in init sage.rings.rational (sage/rings/rational.c:27749)
  File "fast_arith.pyx", line 51, in init sage.rings.fast_arith (sage/rings/fast_arith.c:8051)
  File "integer_ring.pyx", line 67, in init sage.rings.integer_ring (sage/rings/integer_ring.c:12156)
  File "/Applications/sage-5.9/local/lib/python2.7/site-packages/sage/structure/", line 188, in <module>
    from sage.misc.all import prod
  File "/Applications/sage-5.9/local/lib/python2.7/site-packages/sage/misc/", line 85, in <module>
    from functional import (additive_order,
  File "/Applications/sage-5.9/local/lib/python2.7/site-packages/sage/misc/", line 36, in <module>
    from sage.rings.complex_double import CDF
  File "complex_double.pyx", line 87, in init sage.rings.complex_double (sage/rings/complex_double.c:16745)
  File "real_mpfr.pxd", line 15, in init sage.rings.complex_number (sage/rings/complex_number.c:18304)
  File "real_mpfr.pyx", line 1, in init sage.rings.real_mpfr (sage/rings/real_mpfr.c:33344)
  File "utils.pyx", line 11, in init sage.libs.mpmath.utils (sage/libs/mpmath/utils.c:6633)
  File "/Applications/sage-5.9/local/lib/python2.7/site-packages/sage/", line 86, in <module>
    from sage.libs.all       import *
  File "/Applications/sage-5.9/local/lib/python2.7/site-packages/sage/libs/", line 1, in <module>
    import sage.libs.ntl.all  as ntl
  File "/Applications/sage-5.9/local/lib/python2.7/site-packages/sage/libs/ntl/", line 1, in <module>
    import all
  File "/Applications/sage-5.9/local/lib/python2.7/site-packages/sage/libs/ntl/", line 26, in <module>
    from sage.libs.ntl.ntl_ZZ import (
  File "ntl_ZZ.pyx", line 24, in init sage.libs.ntl.ntl_ZZ (sage/libs/ntl/ntl_ZZ.cpp:7011)
ImportError: cannot import name IntegerRing

I just stumbled upon local/bin/sage-eval which imports this at the beginning:

import sys
from sage.all import *
from sage.calculus.predefined import x
from sage.misc.preparser import preparse

When I do ...

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-05-31 04:56:50 -0500

Jesustc gravatar image

It is enough to add to

from sage.all import *
from sage.matrix.matrix2 import Matrix

Alternatively, the 2nd line (yours) is enough if you rename your file to sageruntest.sage (then it will be preparsed by Sage).

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools


Asked: 2013-05-31 02:25:44 -0500

Seen: 603 times

Last updated: May 31 '13