Ask Your Question
3

Error importing ZZ

asked 2016-11-11 11:40:15 +0100

MvG gravatar image

I'm trying to switch to a more software-development-like approach for one of my projects. To this end I'll be writing several files, and I'll be trying to keep imports to a minimum to speed up module loading.

At first I started with a file foo.sage and a Makefile which preparses this using sage -min -preparse foo.sage. But the resulting foo.sage.py still starts with from sage.all_cmdline import *. I thought the point of the -min switch was to avoid just that. Am I missing something here?

Next I tried to write Python code instead. But there I got problems, apparently because I was loading modules in the wrong order. Take for example a file foo.py containing just the line from sage.rings.integer_ring import ZZ. My Sage 7.4 on Gentoo will print the following when running said file as sage foo.py:

Traceback (most recent call last):
  File "foo.py", line 1, in <module>
    from sage.rings.integer_ring import ZZ
  File "sage/rings/integer.pxd", line 7, in init sage.rings.integer_ring (…/rings/integer_ring.c:14426)
  File "sage/rings/rational.pxd", line 8, in init sage.rings.integer (…/rings/integer.c:49048)
  File "sage/rings/fast_arith.pxd", line 3, in init sage.rings.rational (…/rings/rational.c:36533)
  File "sage/libs/pari/gen.pxd", line 5, in init sage.rings.fast_arith (…/rings/fast_arith.c:8139)
  File "sage/libs/pari/gen.pyx", line 91, in init sage.libs.pari.gen (…/libs/pari/gen.c:135191)
  File "/usr/lib64/python2.7/site-packages/sage/rings/infinity.py", line 228, in <module>
    from sage.rings.integer_ring import ZZ
ImportError: cannot import name ZZ

Is there a way to reasonably import things like this without too much experimentation, and without importing far more than I actually need here?

edit retag flag offensive close merge delete

Comments

This is a very good question and could really use an answer from an expert!

ml9nn gravatar imageml9nn ( 2018-02-03 17:58:17 +0100 )edit

1 Answer

Sort by » oldest newest most voted
2

answered 2017-12-12 04:41:27 +0100

slelievre gravatar image

updated 2017-12-12 05:14:12 +0100

You first need to import sage.all.

The following should work (I'm including version info for reference):

$ sage -v
SageMath version 8.1, Release Date: 2017-12-07
$ sage -python
Python 2.7.14 (default, Dec  9 2017, 17:25:34) 
[GCC 7.2.0] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sage.all
>>> from sage.rings.integer_ring import ZZ
>>>

Edit: you could even directly import ZZ form sage.all:

$ sage -python 
Python 2.7.14 (default, Dec  9 2017, 17:25:34) 
[GCC 7.2.0] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from sage.all import ZZ
>>>

References:

edit flag offensive delete link more

Comments

For me, it takes about 3.5 seconds to from sage.all import * and about 3.5 seconds to import sage.all and about 3.5 seconds to from sage.all import ZZ, which makes this suggested strategy defeat the purpose of loading modules individually as-needed in order to save time.

ml9nn gravatar imageml9nn ( 2018-02-03 17:44:16 +0100 )edit

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

Stats

Asked: 2016-11-11 11:40:15 +0100

Seen: 2,088 times

Last updated: Dec 12 '17