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.Wed, 21 Dec 2011 20:18:47 +0100Programming with Sage: defining modulehttps://ask.sagemath.org/question/8585/programming-with-sage-defining-module/Let's say I want to write two modules 'addition.sage' and 'result.sage'.
I would like to import 'addition.sage' in 'result.sage'. How can I do this?
I've try the following, but it doesn't seem to work.
'addition.sage' file:
`from sage.all import *`
`add(x,y)=x+y`
'result.sage' file:
`from addition import add`
`print add(1,2)`
my sage session:
`sage: load('myPath/result.sage')`
This gives me the error 'ImportError: No module named addition'.
Although, my PYTHONPATH contains the directory where my two modules are located.Wed, 21 Dec 2011 15:52:47 +0100https://ask.sagemath.org/question/8585/programming-with-sage-defining-module/Answer by Shashank for <p>Let's say I want to write two modules 'addition.sage' and 'result.sage'. <br/>
I would like to import 'addition.sage' in 'result.sage'. How can I do this? <br/>
I've try the following, but it doesn't seem to work.</p>
<p>'addition.sage' file: <br/>
<code>from sage.all import *</code> <br/>
<code>add(x,y)=x+y</code> </p>
<p>'result.sage' file: <br/>
<code>from addition import add</code> <br/>
<code>print add(1,2)</code> </p>
<p>my sage session: <br/>
<code>sage: load('myPath/result.sage')</code></p>
<p>This gives me the error 'ImportError: No module named addition'. <br/>
Although, my PYTHONPATH contains the directory where my two modules are located.</p>
https://ask.sagemath.org/question/8585/programming-with-sage-defining-module/?answer=13054#post-id-13054If result.sage is calling add function from addition.sage, you need to load that file in result.sage.
You can also load the file without giving the path, if you type the following two lines in the beginning of the notebook
import sys
sys.path.append('path-to-module')Wed, 21 Dec 2011 16:28:02 +0100https://ask.sagemath.org/question/8585/programming-with-sage-defining-module/?answer=13054#post-id-13054Comment by Nicolas Essis-Breton for <p>If result.sage is calling add function from addition.sage, you need to load that file in result.sage.</p>
<p>You can also load the file without giving the path, if you type the following two lines in the beginning of the notebook</p>
<pre><code>import sys
sys.path.append('path-to-module')
</code></pre>
https://ask.sagemath.org/question/8585/programming-with-sage-defining-module/?comment=20665#post-id-20665@Shashank Thanks Shashank.Wed, 21 Dec 2011 20:18:47 +0100https://ask.sagemath.org/question/8585/programming-with-sage-defining-module/?comment=20665#post-id-20665