ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 02 Jul 2019 14:39:25 +0200Creating a module in Sagehttps://ask.sagemath.org/question/46994/creating-a-module-in-sage/I am new to Sage, being used to Python and I am having some trouble with adapting.
In particular, I am trying to create a 'module' in the Python sense, i.e. some set of classes that I can call with 'import'.
When I did this in python in the past, I would create a directory (say /my_module/ in my home) and include a file __'__init____.sage' in that file with a line
__all__ = ['Submodule1', 'Submodule2']
Then I would have a file 'Submodule1' and 'Submodule2'.
In the file 'Submodule 1' I would define 'Class1'
and then from a file 'run.py' in home I would write
import my_module
from my_module.Submodule1 import Class1
and then I would be able to write
c=Class1()
to create an object.
I find that this is not working in Sage and the only thing I seem to be able to do is write in run.py
load("my_module/Submodule1.sage")
for each class I want to load. This has many disadvantages, for instance hiding classes from potential users so that they only exist internally.
Any suggestions on how to create and import modules in sage? What is the sage way?
Tue, 25 Jun 2019 19:27:38 +0200https://ask.sagemath.org/question/46994/creating-a-module-in-sage/Answer by slelievre for <p>I am new to Sage, being used to Python and I am having some trouble with adapting.</p>
<p>In particular, I am trying to create a 'module' in the Python sense, i.e. some set of classes that I can call with 'import'.</p>
<p>When I did this in python in the past, I would create a directory (say /my_module/ in my home) and include a file __'__init____.sage' in that file with a line</p>
<pre><code>__all__ = ['Submodule1', 'Submodule2']
</code></pre>
<p>Then I would have a file 'Submodule1' and 'Submodule2'.
In the file 'Submodule 1' I would define 'Class1'</p>
<p>and then from a file 'run.py' in home I would write</p>
<pre><code>import my_module
from my_module.Submodule1 import Class1
</code></pre>
<p>and then I would be able to write</p>
<pre><code>c=Class1()
</code></pre>
<p>to create an object.</p>
<p>I find that this is not working in Sage and the only thing I seem to be able to do is write in run.py</p>
<pre><code>load("my_module/Submodule1.sage")
</code></pre>
<p>for each class I want to load. This has many disadvantages, for instance hiding classes from potential users so that they only exist internally.</p>
<p>Any suggestions on how to create and import modules in sage? What is the sage way?</p>
https://ask.sagemath.org/question/46994/creating-a-module-in-sage/?answer=47005#post-id-47005Instead of `.sage` files, use `.py` files. Then everything works pythonically.
The difference between `.sage` files and `.py` files is that when Sage
reads `.py` files no preparsing is applied and all imports must be explicit.
For example, if you want to use `matrix` in such a file, you will have to use
from sage.matrix.constructor import matrix
Note that one can easily know what import statements to include, by using
the `import_statements` function in a Sage session:
sage: import_statements('matrix')
from sage.matrix.constructor import matrix
sage: import_statements('ZZ')
from sage.rings.integer_ring import ZZ
and one can easily know how to replace Sage-specific constructs that take
advantage of the Sage preparser, by asking Sage how they are preparsed:
sage: print(preparse('''
....: for i in (1 .. 7):
....: print(i)
....: '''))
for i in (ellipsis_iter(Integer(1), Ellipsis, Integer(7))):
print(i)
sage: print(preparse('R.<x> = PolynomialRing(QQ, 2)'))
R = PolynomialRing(QQ, Integer(2), names=('x',)); (x,) = R._first_ngens(1)Tue, 25 Jun 2019 23:16:17 +0200https://ask.sagemath.org/question/46994/creating-a-module-in-sage/?answer=47005#post-id-47005Comment by Iguananaut for <p>Instead of <code>.sage</code> files, use <code>.py</code> files. Then everything works pythonically.</p>
<p>The difference between <code>.sage</code> files and <code>.py</code> files is that when Sage
reads <code>.py</code> files no preparsing is applied and all imports must be explicit.</p>
<p>For example, if you want to use <code>matrix</code> in such a file, you will have to use</p>
<pre><code>from sage.matrix.constructor import matrix
</code></pre>
<p>Note that one can easily know what import statements to include, by using
the <code>import_statements</code> function in a Sage session:</p>
<pre><code>sage: import_statements('matrix')
from sage.matrix.constructor import matrix
sage: import_statements('ZZ')
from sage.rings.integer_ring import ZZ
</code></pre>
<p>and one can easily know how to replace Sage-specific constructs that take
advantage of the Sage preparser, by asking Sage how they are preparsed:</p>
<pre><code>sage: print(preparse('''
....: for i in (1 .. 7):
....: print(i)
....: '''))
for i in (ellipsis_iter(Integer(1), Ellipsis, Integer(7))):
print(i)
sage: print(preparse('R.<x> = PolynomialRing(QQ, 2)'))
R = PolynomialRing(QQ, Integer(2), names=('x',)); (x,) = R._first_ngens(1)
</code></pre>
https://ask.sagemath.org/question/46994/creating-a-module-in-sage/?comment=47038#post-id-47038Note: There is also work in-progress to make `.sage` modules importable like normal Python modules. In this case they would work exactly like `.py` files except that they would be passed through the Sage pre-processor when you import them. However, we don't know yet when that functionality will actually be available: https://trac.sagemath.org/ticket/27074Tue, 02 Jul 2019 14:39:25 +0200https://ask.sagemath.org/question/46994/creating-a-module-in-sage/?comment=47038#post-id-47038