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.Sat, 11 Mar 2017 20:53:12 +0100[RESOLVED]Use sage objects in python module fileshttps://ask.sagemath.org/question/36850/resolveduse-sage-objects-in-python-module-files/ Hello,
I'm trying to figure out how to use sage functions/classes in a module .py file, from another python file.
Here's the thing, I'm developing a small simulator in Python 2 intended to be use with sage for solving diff. equations, etc...
So I have different .py files (interface, calcul, etc) in which have been written some code using sage functions.
Let's say I have "y = var('y')" in calcul.py and I run it from sage console ->
sage: load("calcul.py")
sage:
All works properly without any failures.
But, if I want to import this module in interface.py and then run the file with sage, I get this error ->
sage: load("interface.py")
---> 1 y = var('y')
NameError: name 'var' is not defined
As if it didn't recognize its own functions.
Is there any command/function to use in sage to allow loading of imported files without that type of error ?
Thanks in advance for your help.
Tue, 07 Mar 2017 13:34:02 +0100https://ask.sagemath.org/question/36850/resolveduse-sage-objects-in-python-module-files/Answer by maaaaaaartin for <p>Hello,</p>
<p>I'm trying to figure out how to use sage functions/classes in a module .py file, from another python file.
Here's the thing, I'm developing a small simulator in Python 2 intended to be use with sage for solving diff. equations, etc...
So I have different .py files (interface, calcul, etc) in which have been written some code using sage functions.</p>
<p>Let's say I have "y = var('y')" in calcul.py and I run it from sage console -> </p>
<p>sage: load("calcul.py")
sage:</p>
<p>All works properly without any failures.
But, if I want to import this module in interface.py and then run the file with sage, I get this error -></p>
<p>sage: load("interface.py")
---> 1 y = var('y')</p>
<p>NameError: name 'var' is not defined</p>
<p>As if it didn't recognize its own functions.
Is there any command/function to use in sage to allow loading of imported files without that type of error ?</p>
<p>Thanks in advance for your help. </p>
https://ask.sagemath.org/question/36850/resolveduse-sage-objects-in-python-module-files/?answer=36877#post-id-36877My advice: put `import sage.all as sage` at the beginning of each python file, and then use for instance sage.var('x') in your scripts.Thu, 09 Mar 2017 12:19:09 +0100https://ask.sagemath.org/question/36850/resolveduse-sage-objects-in-python-module-files/?answer=36877#post-id-36877Answer by Hyorvenn for <p>Hello,</p>
<p>I'm trying to figure out how to use sage functions/classes in a module .py file, from another python file.
Here's the thing, I'm developing a small simulator in Python 2 intended to be use with sage for solving diff. equations, etc...
So I have different .py files (interface, calcul, etc) in which have been written some code using sage functions.</p>
<p>Let's say I have "y = var('y')" in calcul.py and I run it from sage console -> </p>
<p>sage: load("calcul.py")
sage:</p>
<p>All works properly without any failures.
But, if I want to import this module in interface.py and then run the file with sage, I get this error -></p>
<p>sage: load("interface.py")
---> 1 y = var('y')</p>
<p>NameError: name 'var' is not defined</p>
<p>As if it didn't recognize its own functions.
Is there any command/function to use in sage to allow loading of imported files without that type of error ?</p>
<p>Thanks in advance for your help. </p>
https://ask.sagemath.org/question/36850/resolveduse-sage-objects-in-python-module-files/?answer=36916#post-id-36916Thanks a lot for your answers !Sat, 11 Mar 2017 16:22:28 +0100https://ask.sagemath.org/question/36850/resolveduse-sage-objects-in-python-module-files/?answer=36916#post-id-36916Answer by eric_g for <p>Hello,</p>
<p>I'm trying to figure out how to use sage functions/classes in a module .py file, from another python file.
Here's the thing, I'm developing a small simulator in Python 2 intended to be use with sage for solving diff. equations, etc...
So I have different .py files (interface, calcul, etc) in which have been written some code using sage functions.</p>
<p>Let's say I have "y = var('y')" in calcul.py and I run it from sage console -> </p>
<p>sage: load("calcul.py")
sage:</p>
<p>All works properly without any failures.
But, if I want to import this module in interface.py and then run the file with sage, I get this error -></p>
<p>sage: load("interface.py")
---> 1 y = var('y')</p>
<p>NameError: name 'var' is not defined</p>
<p>As if it didn't recognize its own functions.
Is there any command/function to use in sage to allow loading of imported files without that type of error ?</p>
<p>Thanks in advance for your help. </p>
https://ask.sagemath.org/question/36850/resolveduse-sage-objects-in-python-module-files/?answer=36852#post-id-36852Indeed you have to add
from sage.calculus.var import var
in `calcul.py` and do the same thing for any Sage name that appears in your module.To know the precise import statement to use, run `import_statements` in a Sage console:
sage: import_statements(var)
from sage.calculus.var import var
sage: import_statements(SR)
from sage.symbolic.ring import SR
sage: import_statements(pi)
from sage.symbolic.constants import pi
Tue, 07 Mar 2017 21:13:03 +0100https://ask.sagemath.org/question/36850/resolveduse-sage-objects-in-python-module-files/?answer=36852#post-id-36852Comment by nbruin for <p>Indeed you have to add </p>
<pre><code>from sage.calculus.var import var
</code></pre>
<p>in <code>calcul.py</code> and do the same thing for any Sage name that appears in your module.To know the precise import statement to use, run <code>import_statements</code> in a Sage console:</p>
<pre><code>sage: import_statements(var)
from sage.calculus.var import var
sage: import_statements(SR)
from sage.symbolic.ring import SR
sage: import_statements(pi)
from sage.symbolic.constants import pi
</code></pre>
https://ask.sagemath.org/question/36850/resolveduse-sage-objects-in-python-module-files/?comment=36853#post-id-36853And as the documentation of `var` explains, in a programming/library environment you're better off using `y=SR.var('y')`. The `var` function that is used on top-level has some nasty side-effects that are convenient for interactive use, but are bad in library code.Tue, 07 Mar 2017 21:21:08 +0100https://ask.sagemath.org/question/36850/resolveduse-sage-objects-in-python-module-files/?comment=36853#post-id-36853Comment by eric_g for <p>Indeed you have to add </p>
<pre><code>from sage.calculus.var import var
</code></pre>
<p>in <code>calcul.py</code> and do the same thing for any Sage name that appears in your module.To know the precise import statement to use, run <code>import_statements</code> in a Sage console:</p>
<pre><code>sage: import_statements(var)
from sage.calculus.var import var
sage: import_statements(SR)
from sage.symbolic.ring import SR
sage: import_statements(pi)
from sage.symbolic.constants import pi
</code></pre>
https://ask.sagemath.org/question/36850/resolveduse-sage-objects-in-python-module-files/?comment=36854#post-id-36854Indeed! Thanks for the correction.Tue, 07 Mar 2017 21:29:48 +0100https://ask.sagemath.org/question/36850/resolveduse-sage-objects-in-python-module-files/?comment=36854#post-id-36854Comment by maaaaaaartin for <p>Indeed you have to add </p>
<pre><code>from sage.calculus.var import var
</code></pre>
<p>in <code>calcul.py</code> and do the same thing for any Sage name that appears in your module.To know the precise import statement to use, run <code>import_statements</code> in a Sage console:</p>
<pre><code>sage: import_statements(var)
from sage.calculus.var import var
sage: import_statements(SR)
from sage.symbolic.ring import SR
sage: import_statements(pi)
from sage.symbolic.constants import pi
</code></pre>
https://ask.sagemath.org/question/36850/resolveduse-sage-objects-in-python-module-files/?comment=36878#post-id-36878A pointer to the doc? Thanks!Thu, 09 Mar 2017 12:19:38 +0100https://ask.sagemath.org/question/36850/resolveduse-sage-objects-in-python-module-files/?comment=36878#post-id-36878Comment by mforets for <p>Indeed you have to add </p>
<pre><code>from sage.calculus.var import var
</code></pre>
<p>in <code>calcul.py</code> and do the same thing for any Sage name that appears in your module.To know the precise import statement to use, run <code>import_statements</code> in a Sage console:</p>
<pre><code>sage: import_statements(var)
from sage.calculus.var import var
sage: import_statements(SR)
from sage.symbolic.ring import SR
sage: import_statements(pi)
from sage.symbolic.constants import pi
</code></pre>
https://ask.sagemath.org/question/36850/resolveduse-sage-objects-in-python-module-files/?comment=36917#post-id-36917[src/sage/calculus/var.pyx](https://github.com/sagemath/sage/blob/master/src/sage/calculus/var.pyx)Sat, 11 Mar 2017 20:53:12 +0100https://ask.sagemath.org/question/36850/resolveduse-sage-objects-in-python-module-files/?comment=36917#post-id-36917