Ask Your Question

Revision history [back]

When 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 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.)

When 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.)