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 Dec 2011 13:18:47 -0600Programming with Sage: defining modulehttp://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 08:52:47 -0600http://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>
http://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 09:28:02 -0600http://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>
http://ask.sagemath.org/question/8585/programming-with-sage-defining-module/?comment=20665#post-id-20665@Shashank Thanks Shashank.Wed, 21 Dec 2011 13:18:47 -0600http://ask.sagemath.org/question/8585/programming-with-sage-defining-module/?comment=20665#post-id-20665