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.Mon, 08 Oct 2018 03:06:35 -0500error with importing sage into a python filehttp://ask.sagemath.org/question/43849/error-with-importing-sage-into-a-python-file/Im trying to import a sage package (var) in a python file
from sage.all import var
but when run the file Im getting this error :
Error
Traceback (most recent call last):
execfile("/home/sagefolder/SageMath/myapp/core/test.py")
File "/home/sagefolder/SageMath/ubbr/engine/test.py", line 20, in <module>
from sage.all import var
File "/home/sagefolder/SageMath/local/lib/python2.7/site-packages/sage/all.py", line 71, in <module>
from sage.env import SAGE_ROOT, SAGE_SRC, SAGE_DOC_SRC, SAGE_LOCAL, DOT_SAGE, SAGE_ENV
File "/home/sagefolder/SageMath/local/lib/python2.7/site-packages/sage/env.py", line 123, in <module>
_add_variable_or_fallback('SAGE_ETC', opj('$SAGE_LOCAL', 'etc'))
File "/home/sagefolder/SageMath/local/lib/python2.7/site-packages/sage/env.py", line 103, in _add_variable_or_fallback
value = sep.join(components)
TypeError: sequence item 0: expected string, NoneType found
my guess was that the problems was related to the env path. but even
sys.path.append('/path/to/the/sage/')
it does not work
It seems it has problem with the value of some env variables like 'SAGE_ROOT' and ... but according to the [installation manual][1], if I set the symbolic link
`ln -s /path/to/SageMath/sage /usr/local/bin/sage`
then all the required env variable are automatically handled.
this module is called from a django views.py which is also installed as a Sage package. if I run the the module
using
sage test.py
everything is ok but when it is called from a django view which is also installed as sage packages I got the mentioned error
I would be pleased if anybody can help me as Im doing an important project and I really need to use sage.
[1]: http://doc.sagemath.org/html/en/installation/binary.html#sec-installation-from-binariesSat, 06 Oct 2018 16:31:57 -0500http://ask.sagemath.org/question/43849/error-with-importing-sage-into-a-python-file/Answer by nimaous for <p>Im trying to import a sage package (var) in a python file </p>
<pre><code>from sage.all import var
</code></pre>
<p>but when run the file Im getting this error : </p>
<pre><code>Error
Traceback (most recent call last):
execfile("/home/sagefolder/SageMath/myapp/core/test.py")
File "/home/sagefolder/SageMath/ubbr/engine/test.py", line 20, in <module>
from sage.all import var
File "/home/sagefolder/SageMath/local/lib/python2.7/site-packages/sage/all.py", line 71, in <module>
from sage.env import SAGE_ROOT, SAGE_SRC, SAGE_DOC_SRC, SAGE_LOCAL, DOT_SAGE, SAGE_ENV
File "/home/sagefolder/SageMath/local/lib/python2.7/site-packages/sage/env.py", line 123, in <module>
_add_variable_or_fallback('SAGE_ETC', opj('$SAGE_LOCAL', 'etc'))
File "/home/sagefolder/SageMath/local/lib/python2.7/site-packages/sage/env.py", line 103, in _add_variable_or_fallback
value = sep.join(components)
TypeError: sequence item 0: expected string, NoneType found
</code></pre>
<p>my guess was that the problems was related to the env path. but even </p>
<pre><code>sys.path.append('/path/to/the/sage/')
</code></pre>
<p>it does not work</p>
<p>It seems it has problem with the value of some env variables like 'SAGE_ROOT' and ... but according to the http://doc.sagemath.org/html/en/installation/binary.html#sec-installation-from-binaries (installation manual), if I set the symbolic link </p>
<p><code>ln -s /path/to/SageMath/sage /usr/local/bin/sage</code> </p>
<p>then all the required env variable are automatically handled. </p>
<p>this module is called from a django views.py which is also installed as a Sage package. if I run the the module
using </p>
<pre><code>sage test.py
</code></pre>
<p>everything is ok but when it is called from a django view which is also installed as sage packages I got the mentioned error</p>
<p>I would be pleased if anybody can help me as Im doing an important project and I really need to use sage. </p>
http://ask.sagemath.org/question/43849/error-with-importing-sage-into-a-python-file/?answer=43858#post-id-43858I set the env variables in the python file which is called from djagno views function. and it's solved. you need to set the following variables:
import os
os.environ['SAGE_ROOT'] = 'you can find this path by running os.environ['SAGE_ROOT'] in sage shell
os.environ['SAGE_SRC'] = 'you can find this path by running os.environ['SAGE_ROOT'] in sage shell
os.environ['SAGE_DOC_SRC'] = 'you can find this path by running os.environ['SAGE_ROOT'] in sage shell
os.environ['SAGE_LOCAL'] = 'you can find this path by running os.environ['SAGE_ROOT'] in sage shell
os.environ['DOT_SAGE'] = 'you can find this path by running os.environ['SAGE_ROOT'] in sage shell
Mon, 08 Oct 2018 03:06:35 -0500http://ask.sagemath.org/question/43849/error-with-importing-sage-into-a-python-file/?answer=43858#post-id-43858Answer by eric_g for <p>Im trying to import a sage package (var) in a python file </p>
<pre><code>from sage.all import var
</code></pre>
<p>but when run the file Im getting this error : </p>
<pre><code>Error
Traceback (most recent call last):
execfile("/home/sagefolder/SageMath/myapp/core/test.py")
File "/home/sagefolder/SageMath/ubbr/engine/test.py", line 20, in <module>
from sage.all import var
File "/home/sagefolder/SageMath/local/lib/python2.7/site-packages/sage/all.py", line 71, in <module>
from sage.env import SAGE_ROOT, SAGE_SRC, SAGE_DOC_SRC, SAGE_LOCAL, DOT_SAGE, SAGE_ENV
File "/home/sagefolder/SageMath/local/lib/python2.7/site-packages/sage/env.py", line 123, in <module>
_add_variable_or_fallback('SAGE_ETC', opj('$SAGE_LOCAL', 'etc'))
File "/home/sagefolder/SageMath/local/lib/python2.7/site-packages/sage/env.py", line 103, in _add_variable_or_fallback
value = sep.join(components)
TypeError: sequence item 0: expected string, NoneType found
</code></pre>
<p>my guess was that the problems was related to the env path. but even </p>
<pre><code>sys.path.append('/path/to/the/sage/')
</code></pre>
<p>it does not work</p>
<p>It seems it has problem with the value of some env variables like 'SAGE_ROOT' and ... but according to the http://doc.sagemath.org/html/en/installation/binary.html#sec-installation-from-binaries (installation manual), if I set the symbolic link </p>
<p><code>ln -s /path/to/SageMath/sage /usr/local/bin/sage</code> </p>
<p>then all the required env variable are automatically handled. </p>
<p>this module is called from a django views.py which is also installed as a Sage package. if I run the the module
using </p>
<pre><code>sage test.py
</code></pre>
<p>everything is ok but when it is called from a django view which is also installed as sage packages I got the mentioned error</p>
<p>I would be pleased if anybody can help me as Im doing an important project and I really need to use sage. </p>
http://ask.sagemath.org/question/43849/error-with-importing-sage-into-a-python-file/?answer=43852#post-id-43852A solution is run Sage's Python (instead of your system's Python):
sage -python
Equivalently, you can run Python from Sage shell:
sage -sh
python
Then all Sage's environment variables are automatically set and `from sage.all import var` does not give any error.
Sun, 07 Oct 2018 07:34:57 -0500http://ask.sagemath.org/question/43849/error-with-importing-sage-into-a-python-file/?answer=43852#post-id-43852Comment by nimaous for <p>A solution is run Sage's Python (instead of your system's Python):</p>
<pre><code>sage -python
</code></pre>
<p>Equivalently, you can run Python from Sage shell:</p>
<pre><code>sage -sh
python
</code></pre>
<p>Then all Sage's environment variables are automatically set and <code>from sage.all import var</code> does not give any error.</p>
http://ask.sagemath.org/question/43849/error-with-importing-sage-into-a-python-file/?comment=43854#post-id-43854thank you for your answer. Indeed Im calling this python module in a Django views.py. actually I'v installed django as a Sage package but it seems there are some problems with Sage env variables.Sun, 07 Oct 2018 12:32:14 -0500http://ask.sagemath.org/question/43849/error-with-importing-sage-into-a-python-file/?comment=43854#post-id-43854