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.Wed, 21 Feb 2018 09:04:13 -0600Getting my own module to work in Sagehttp://ask.sagemath.org/question/41204/getting-my-own-module-to-work-in-sage/ Hey everyone. I wrote a sage module that defines a couple functions that I'd like to be able to import into sage any time I want. From what I've researched, it seems the way to do this is save the file that defines the functions as a .py file, then put it into sage's version of python. I've done that, and sage imports the file just fine without errors, except when I attempt to call functions from the file in the sage prompt, it gets stuck on the first sage-specific function, in this case var(). I get this error message:
sage: polyv1.sift(4, 3)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-20-d64cf99d698f> in <module>()
----> 1 polyv1.sift(Integer(4), Integer(3))
/usr/lib/sagemath/local/lib/python/polyv1.py in sift(r, s)
NameError: global name 'var' is not defined
I've tried adding 'from sage.all import *' to the beginning of the file, and it doesn't create an error, but it also doesn't fix the problem. Am I missing something really easy? How do you get sage to recognize the sage functions?Tue, 20 Feb 2018 14:54:48 -0600http://ask.sagemath.org/question/41204/getting-my-own-module-to-work-in-sage/Answer by tmonteil for <p>Hey everyone. I wrote a sage module that defines a couple functions that I'd like to be able to import into sage any time I want. From what I've researched, it seems the way to do this is save the file that defines the functions as a .py file, then put it into sage's version of python. I've done that, and sage imports the file just fine without errors, except when I attempt to call functions from the file in the sage prompt, it gets stuck on the first sage-specific function, in this case var(). I get this error message:</p>
<pre><code> sage: polyv1.sift(4, 3)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-20-d64cf99d698f> in <module>()
----> 1 polyv1.sift(Integer(4), Integer(3))
/usr/lib/sagemath/local/lib/python/polyv1.py in sift(r, s)
NameError: global name 'var' is not defined
</code></pre>
<p>I've tried adding 'from sage.all import *' to the beginning of the file, and it doesn't create an error, but it also doesn't fix the problem. Am I missing something really easy? How do you get sage to recognize the sage functions?</p>
http://ask.sagemath.org/question/41204/getting-my-own-module-to-work-in-sage/?answer=41205#post-id-41205You have to import what is used in the file. For this, there is a nice tool:
sage: import_statements("var")
from sage.calculus.var import var
then you just have to copy-paste `from sage.calculus.var import var` in the beginning of your file.
Tue, 20 Feb 2018 14:58:29 -0600http://ask.sagemath.org/question/41204/getting-my-own-module-to-work-in-sage/?answer=41205#post-id-41205Comment by trenzafeeds for <p>You have to import what is used in the file. For this, there is a nice tool:</p>
<pre><code>sage: import_statements("var")
from sage.calculus.var import var
</code></pre>
<p>then you just have to copy-paste <code>from sage.calculus.var import var</code> in the beginning of your file.</p>
http://ask.sagemath.org/question/41204/getting-my-own-module-to-work-in-sage/?comment=41220#post-id-41220Turns out it was a mistake that I was making, not a code problem! Sorry for troubling you guys, turns out that first answer was totally correct!Wed, 21 Feb 2018 09:04:13 -0600http://ask.sagemath.org/question/41204/getting-my-own-module-to-work-in-sage/?comment=41220#post-id-41220Comment by Iguananaut for <p>You have to import what is used in the file. For this, there is a nice tool:</p>
<pre><code>sage: import_statements("var")
from sage.calculus.var import var
</code></pre>
<p>then you just have to copy-paste <code>from sage.calculus.var import var</code> in the beginning of your file.</p>
http://ask.sagemath.org/question/41204/getting-my-own-module-to-work-in-sage/?comment=41216#post-id-41216This is all in the same file right? If the import succeeded (i.e. there's no `ImportError`) then you should have anything you imported defined globally (*within the module that imported it*). Beyond that, if you're still getting a `NameError` then there's something else strange in your code that would be hard to pinpoint without seeing all of it.Wed, 21 Feb 2018 07:23:28 -0600http://ask.sagemath.org/question/41204/getting-my-own-module-to-work-in-sage/?comment=41216#post-id-41216Comment by trenzafeeds for <p>You have to import what is used in the file. For this, there is a nice tool:</p>
<pre><code>sage: import_statements("var")
from sage.calculus.var import var
</code></pre>
<p>then you just have to copy-paste <code>from sage.calculus.var import var</code> in the beginning of your file.</p>
http://ask.sagemath.org/question/41204/getting-my-own-module-to-work-in-sage/?comment=41209#post-id-41209For sure. The entire document is pretty long, but I've taken what I think are the most important parts. Let me know if anything else seems important.
Here's the header of the document (everything I'm importing):
from sage.calculus.var import var
import itertools
from sympy.utilities.iterables import multiset_permutations
and here's the beginning of the function that gives the above-mentioned error when I call it:
def sift(r, s):
polynomial = 1
p = r/2
q = (s-1)/2
poly_degree = 0
#For x-terms
for j in xrange(2, r+1):
for i in xrange(1, j):
term = (var("x" + str(j))-var("x" + str(i)))
polynomial = polynomial *term
poly_degree += 1
it's the first 'var("x" + str(j)) there that's giving me the error.Tue, 20 Feb 2018 17:01:00 -0600http://ask.sagemath.org/question/41204/getting-my-own-module-to-work-in-sage/?comment=41209#post-id-41209Comment by tmonteil for <p>You have to import what is used in the file. For this, there is a nice tool:</p>
<pre><code>sage: import_statements("var")
from sage.calculus.var import var
</code></pre>
<p>then you just have to copy-paste <code>from sage.calculus.var import var</code> in the beginning of your file.</p>
http://ask.sagemath.org/question/41204/getting-my-own-module-to-work-in-sage/?comment=41208#post-id-41208Could you please provide your code so that we understand what happens ?Tue, 20 Feb 2018 16:39:39 -0600http://ask.sagemath.org/question/41204/getting-my-own-module-to-work-in-sage/?comment=41208#post-id-41208Comment by trenzafeeds for <p>You have to import what is used in the file. For this, there is a nice tool:</p>
<pre><code>sage: import_statements("var")
from sage.calculus.var import var
</code></pre>
<p>then you just have to copy-paste <code>from sage.calculus.var import var</code> in the beginning of your file.</p>
http://ask.sagemath.org/question/41204/getting-my-own-module-to-work-in-sage/?comment=41206#post-id-41206Unfortunately still getting the same error, any other ideas?Tue, 20 Feb 2018 15:20:34 -0600http://ask.sagemath.org/question/41204/getting-my-own-module-to-work-in-sage/?comment=41206#post-id-41206