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.Tue, 26 Jul 2016 13:46:34 -0500Can I create a sage file, and import it as a Python module?http://ask.sagemath.org/question/33954/can-i-create-a-sage-file-and-import-it-as-a-python-module/ I am trying to create a file that produces some data using sage specific modules (eg graphs. ).
Is there a way that I can save this file and then import it in a Python shell and / or in a Sage shell?
The problems I have encountered so far are:
- if I try to save the file as filename.py and import it as a module in a Python or Sage shell, Python/ Sage won't recognize the sage specific functions (which makes sense)
- if I try to save the file as filename.sage and import it as a module in a Sage shell, I get the error "No module named filename"
I am working from an Ubuntu terminal.
Thank you!Tue, 28 Jun 2016 11:14:23 -0500http://ask.sagemath.org/question/33954/can-i-create-a-sage-file-and-import-it-as-a-python-module/Comment by MargaretMitchel for <p>I am trying to create a file that produces some data using sage specific modules (eg graphs. ).
Is there a way that I can save this file and then import it in a Python shell and / or in a Sage shell?</p>
<p>The problems I have encountered so far are:</p>
<ul>
<li>if I try to save the file as filename.py and import it as a module in a Python or Sage shell, Python/ Sage won't recognize the sage specific functions (which makes sense)</li>
<li>if I try to save the file as filename.sage and import it as a module in a Sage shell, I get the error "No module named filename"</li>
</ul>
<p>I am working from an Ubuntu terminal.</p>
<p>Thank you!</p>
http://ask.sagemath.org/question/33954/can-i-create-a-sage-file-and-import-it-as-a-python-module/?comment=33979#post-id-33979I just know that the standard way to import a file is :
sage: load /path/to/foo.py
or
sage: attach /path/to/foo.py.
But I guess I need to read more about PythonThu, 30 Jun 2016 09:24:06 -0500http://ask.sagemath.org/question/33954/can-i-create-a-sage-file-and-import-it-as-a-python-module/?comment=33979#post-id-33979Answer by slelievre for <p>I am trying to create a file that produces some data using sage specific modules (eg graphs. ).
Is there a way that I can save this file and then import it in a Python shell and / or in a Sage shell?</p>
<p>The problems I have encountered so far are:</p>
<ul>
<li>if I try to save the file as filename.py and import it as a module in a Python or Sage shell, Python/ Sage won't recognize the sage specific functions (which makes sense)</li>
<li>if I try to save the file as filename.sage and import it as a module in a Sage shell, I get the error "No module named filename"</li>
</ul>
<p>I am working from an Ubuntu terminal.</p>
<p>Thank you!</p>
http://ask.sagemath.org/question/33954/can-i-create-a-sage-file-and-import-it-as-a-python-module/?answer=33955#post-id-33955The solution is to use a filename.py file.
For the sage specific syntax, find the corresponding Python code using `preparse`.
Find the sage specific functions to import using `import_statements`.
For example, suppose that your sage file has
R.<x> = PolynomialRing(QQ)
which is not correct Python syntax. Check how to turn that into correct Python.
sage: preparse('R.<x> = PolynomialRing(QQ)')
"R = PolynomialRing(QQ, names=('x',)); (x,) = R._first_ngens(1)"
so in filename.py you would write
R = PolynomialRing(QQ, names=('x',))
(x,) = R._first_ngens(1)
instead of
R.<x> = PolynomialRing(QQ)
You would also need the import statements, which you find with
sage: import_statements('PolynomialRing')
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
sage: import_statements('QQ')
...
from sage.rings.rational_field import QQ
So your python file would really have
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.rings.rational_field import QQ
R = PolynomialRing(QQ, names=('x',))
(x,) = R._first_ngens(1)
Tue, 28 Jun 2016 16:54:50 -0500http://ask.sagemath.org/question/33954/can-i-create-a-sage-file-and-import-it-as-a-python-module/?answer=33955#post-id-33955Comment by jaebond for <p>The solution is to use a filename.py file.</p>
<p>For the sage specific syntax, find the corresponding Python code using <code>preparse</code>.</p>
<p>Find the sage specific functions to import using <code>import_statements</code>.</p>
<p>For example, suppose that your sage file has</p>
<pre><code>R.<x> = PolynomialRing(QQ)
</code></pre>
<p>which is not correct Python syntax. Check how to turn that into correct Python.</p>
<pre><code>sage: preparse('R.<x> = PolynomialRing(QQ)')
"R = PolynomialRing(QQ, names=('x',)); (x,) = R._first_ngens(1)"
</code></pre>
<p>so in filename.py you would write</p>
<pre><code>R = PolynomialRing(QQ, names=('x',))
(x,) = R._first_ngens(1)
</code></pre>
<p>instead of</p>
<pre><code>R.<x> = PolynomialRing(QQ)
</code></pre>
<p>You would also need the import statements, which you find with</p>
<pre><code>sage: import_statements('PolynomialRing')
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
sage: import_statements('QQ')
...
from sage.rings.rational_field import QQ
</code></pre>
<p>So your python file would really have</p>
<pre><code>from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.rings.rational_field import QQ
R = PolynomialRing(QQ, names=('x',))
(x,) = R._first_ngens(1)
</code></pre>
http://ask.sagemath.org/question/33954/can-i-create-a-sage-file-and-import-it-as-a-python-module/?comment=34240#post-id-34240For the syntax, you can run `sage -preparse file.sage` from the command line and it will do all of the syntax conversions for you. It will also add the line `from sage.all import *` at the top, though you have to then run this with Sage's version of Python (or tell Python where to find `sage.all`).Tue, 26 Jul 2016 13:46:34 -0500http://ask.sagemath.org/question/33954/can-i-create-a-sage-file-and-import-it-as-a-python-module/?comment=34240#post-id-34240