Ask Your Question

How to correctly import sage modules from python mode

asked 2020-09-17 09:58:04 +0200

hellman gravatar image

I am writing a module for sage, and I want to import some sage objects cleanly, without calling the heavy import sage.all. But without it, I often get errors.

$ sage -python
Python 3.7.3 (default, May 21 2020, 07:31:27) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sage.rings.integer import Integer
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "sage/rings/rational.pxd", line 8, in init sage.rings.integer (build/cythonized/sage/rings/integer.c:53202)
  File "sage/rings/rational.pyx", line 95, in init sage.rings.rational (build/cythonized/sage/rings/rational.c:39849)
  File "sage/rings/real_mpfr.pyx", line 1, in init sage.rings.real_mpfr (build/cythonized/sage/rings/real_mpfr.c:44200)
  File "sage/rings/complex_number.pxd", line 6, in init sage.libs.mpmath.utils (build/cythonized/sage/libs/mpmath/utils.c:8637)
  File "sage/rings/complex_double.pxd", line 7, in init sage.rings.complex_number (build/cythonized/sage/rings/complex_number.c:24172)
  File "sage/rings/complex_double.pyx", line 96, in init sage.rings.complex_double (build/cythonized/sage/rings/complex_double.c:24325)
ImportError: cannot import name complex_number
>>> from sage.all import *
>>> from sage.rings.integer import Integer
$ sage
│ SageMath version 9.1, Release Date: 2020-05-20                     │
│ Using Python 3.7.3. Type "help()" for help.                        │

This is just an example.

edit retag flag offensive close merge delete



I would be tempted to say that there is no other way than starting with the import of sage.all.

FrédéricC gravatar imageFrédéricC ( 2020-09-17 20:10:50 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2020-09-17 21:12:08 +0200

Emmanuel Charpentier gravatar image

Ahem :

charpent@zen-book-flip:~$ sage -python
Python 3.8.5 (default, Aug  2 2020, 15:09:07) 
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sage import Integer
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'Integer' from 'sage' (/usr/local/sage-9/local/lib/python3.8/site-packages/sage/

fails indeed. But :

>>> from sage.all import Integer

succeeds. And pulls with it a large part of Sage :

>>> foo=Integer(3)
>>> bar=foo+2
>>> bar
>>> bar.parent()
Integer Ring

but not the preprocessor :

>>> 2^3
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

1 follower


Asked: 2020-09-17 09:58:04 +0200

Seen: 1,158 times

Last updated: Sep 17 '20