ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 03 Feb 2018 10:58:17 -0600Error importing ZZhttp://ask.sagemath.org/question/35522/error-importing-zz/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?Fri, 11 Nov 2016 04:40:15 -0600http://ask.sagemath.org/question/35522/error-importing-zz/Comment by ml9nn for <p>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.</p>
<p>At first I started with a file <code>foo.sage</code> and a <code>Makefile</code> which preparses this using <code>sage -min -preparse foo.sage</code>. But the resulting <code>foo.sage.py</code> still starts with <code>from sage.all_cmdline import *</code>. I thought the point of the <code>-min</code> switch was to avoid just that. Am I missing something here?</p>
<p>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 <code>foo.py</code> containing just the line <code>from sage.rings.integer_ring import ZZ</code>. My Sage 7.4 on Gentoo will print the following when running said file as <code>sage foo.py</code>:</p>
<pre><code>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
</code></pre>
<p>Is there a way to reasonably import things like this without too much experimentation, and without importing far more than I actually need here?</p>
http://ask.sagemath.org/question/35522/error-importing-zz/?comment=40957#post-id-40957This is a very good question and could really use an answer from an expert!Sat, 03 Feb 2018 10:58:17 -0600http://ask.sagemath.org/question/35522/error-importing-zz/?comment=40957#post-id-40957Answer by slelievre for <p>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.</p>
<p>At first I started with a file <code>foo.sage</code> and a <code>Makefile</code> which preparses this using <code>sage -min -preparse foo.sage</code>. But the resulting <code>foo.sage.py</code> still starts with <code>from sage.all_cmdline import *</code>. I thought the point of the <code>-min</code> switch was to avoid just that. Am I missing something here?</p>
<p>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 <code>foo.py</code> containing just the line <code>from sage.rings.integer_ring import ZZ</code>. My Sage 7.4 on Gentoo will print the following when running said file as <code>sage foo.py</code>:</p>
<pre><code>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
</code></pre>
<p>Is there a way to reasonably import things like this without too much experimentation, and without importing far more than I actually need here?</p>
http://ask.sagemath.org/question/35522/error-importing-zz/?answer=40118#post-id-40118You 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:
- [2015-01-28 post by Jeroen Demeyer on sage-support](https://groups.google.com/d/msg/sage-support/3lXInDgHJe4/SdOpuuOkqPUJ)
- [2015-02-10 post by Nicolas M. Thiéry on sage-combinat-devel](https://groups.google.com/d/msg/sage-combinat-devel/oi4VtGr1g5A/B8PE7ra8krAJ)Mon, 11 Dec 2017 21:41:27 -0600http://ask.sagemath.org/question/35522/error-importing-zz/?answer=40118#post-id-40118Comment by ml9nn for <p>You first need to <code>import sage.all</code>.</p>
<p>The following should work (I'm including version info for reference):</p>
<pre><code>$ 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
>>>
</code></pre>
<p>Edit: you could even directly import <code>ZZ</code> form <code>sage.all</code>:</p>
<pre><code>$ 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
>>>
</code></pre>
<p>References:</p>
<ul>
<li><p><a href="https://groups.google.com/d/msg/sage-support/3lXInDgHJe4/SdOpuuOkqPUJ">2015-01-28 post by Jeroen Demeyer on sage-support</a></p></li>
<li><p><a href="https://groups.google.com/d/msg/sage-combinat-devel/oi4VtGr1g5A/B8PE7ra8krAJ">2015-02-10 post by Nicolas M. Thiéry on sage-combinat-devel</a></p></li>
</ul>
http://ask.sagemath.org/question/35522/error-importing-zz/?comment=40956#post-id-40956For 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.Sat, 03 Feb 2018 10:44:16 -0600http://ask.sagemath.org/question/35522/error-importing-zz/?comment=40956#post-id-40956