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.Fri, 21 Feb 2020 18:18:06 +0100Error in calling a custom function from sagehttps://ask.sagemath.org/question/50004/error-in-calling-a-custom-function-from-sage/I used [this](https://ask.sagemath.org/question/10723/manage-my-functions-in-sage/) guide to add module containing some utility to the sage.
My module (`myfunc.py`) looks like this:
def perm(n, r):
return factorial(n)/factorial(n-r)
def comb(n, r):
return perm(n, r)/factorial(r)
To import the module I use the following `import` statement:
sage: from myfunc import perm
So far so good.
When I call the `perm()` or `comb()` function. I get the following error:
1 def perm(n, r):
----> 2 return factorial(n)/factorial(n-r)
3
4 def comb(n, r):
5 return perm(n, r)/factorial(r)
NameError: name 'factorial' is not defined
Thu, 20 Feb 2020 15:21:09 +0100https://ask.sagemath.org/question/50004/error-in-calling-a-custom-function-from-sage/Answer by rburing for <p>I used <a href="https://ask.sagemath.org/question/10723/manage-my-functions-in-sage/">this</a> guide to add module containing some utility to the sage.</p>
<p>My module (<code>myfunc.py</code>) looks like this:</p>
<pre><code>def perm(n, r):
return factorial(n)/factorial(n-r)
def comb(n, r):
return perm(n, r)/factorial(r)
</code></pre>
<p>To import the module I use the following <code>import</code> statement:</p>
<pre><code>sage: from myfunc import perm
</code></pre>
<p>So far so good.</p>
<p>When I call the <code>perm()</code> or <code>comb()</code> function. I get the following error:</p>
<pre><code> 1 def perm(n, r):
----> 2 return factorial(n)/factorial(n-r)
3
4 def comb(n, r):
5 return perm(n, r)/factorial(r)
NameError: name 'factorial' is not defined
</code></pre>
https://ask.sagemath.org/question/50004/error-in-calling-a-custom-function-from-sage/?answer=50005#post-id-50005When you create a Python module and you want to use external code (such as `factorial`), you have to import it explicitly (another difference is that you do not have the luxury of SageMath's preparser, as mentioned in the linked post).
This is for namespace reasons; if it didn't work this way then variables could easily get mixed up across modules, and life would be hell.
So you have to find out where `factorial` lives. In a SageMath session, entering `factorial?` will also show you the file it's located in, which is `(...)/site-packages/sage/functions/other.py`. Since `site-packages` is always in `sys.path`, we can import `factorial` as follows:
from sage.functions.other import factorial
Adding this line to the top of your module will fix your problem.
(By the way, there is already a `binomial` function.)Thu, 20 Feb 2020 16:16:36 +0100https://ask.sagemath.org/question/50004/error-in-calling-a-custom-function-from-sage/?answer=50005#post-id-50005Comment by gg for <p>When you create a Python module and you want to use external code (such as <code>factorial</code>), you have to import it explicitly (another difference is that you do not have the luxury of SageMath's preparser, as mentioned in the linked post).
This is for namespace reasons; if it didn't work this way then variables could easily get mixed up across modules, and life would be hell.</p>
<p>So you have to find out where <code>factorial</code> lives. In a SageMath session, entering <code>factorial?</code> will also show you the file it's located in, which is <code>(...)/site-packages/sage/functions/other.py</code>. Since <code>site-packages</code> is always in <code>sys.path</code>, we can import <code>factorial</code> as follows:</p>
<pre><code>from sage.functions.other import factorial
</code></pre>
<p>Adding this line to the top of your module will fix your problem.</p>
<p>(By the way, there is already a <code>binomial</code> function.)</p>
https://ask.sagemath.org/question/50004/error-in-calling-a-custom-function-from-sage/?comment=50006#post-id-50006Thanks for the reply, my module is working now. I discovered the `binomial()` function after posting the question. Is there `binomcdf()` or something similar available too?Thu, 20 Feb 2020 16:28:39 +0100https://ask.sagemath.org/question/50004/error-in-calling-a-custom-function-from-sage/?comment=50006#post-id-50006Comment by gg for <p>When you create a Python module and you want to use external code (such as <code>factorial</code>), you have to import it explicitly (another difference is that you do not have the luxury of SageMath's preparser, as mentioned in the linked post).
This is for namespace reasons; if it didn't work this way then variables could easily get mixed up across modules, and life would be hell.</p>
<p>So you have to find out where <code>factorial</code> lives. In a SageMath session, entering <code>factorial?</code> will also show you the file it's located in, which is <code>(...)/site-packages/sage/functions/other.py</code>. Since <code>site-packages</code> is always in <code>sys.path</code>, we can import <code>factorial</code> as follows:</p>
<pre><code>from sage.functions.other import factorial
</code></pre>
<p>Adding this line to the top of your module will fix your problem.</p>
<p>(By the way, there is already a <code>binomial</code> function.)</p>
https://ask.sagemath.org/question/50004/error-in-calling-a-custom-function-from-sage/?comment=50017#post-id-50017@rburing Okay, I found most of the statistical related functions in `scipy`.Fri, 21 Feb 2020 18:18:06 +0100https://ask.sagemath.org/question/50004/error-in-calling-a-custom-function-from-sage/?comment=50017#post-id-50017