将 sage 导入 python 文件时出错

error with importing sage into a python file

我正在尝试在 python 文件中导入 sage 包 (var)

from sage.all import var 

但是当 运行 文件出现此错误时:

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

我的猜测是问题与 env 路径有关。但即使

sys.path.append('/path/to/the/sage/')

没用

似乎某些环境变量的值有问题,例如 'SAGE_ROOT' 和...但是根据 installation manual,如果我设置符号 link

ln -s /path/to/SageMath/sage /usr/local/bin/sage

然后所有需要的环境变量都会自动处理。

这个模块是从 django views.py 调用的,它也作为 Sage 包安装。如果我 运行 模块 使用

sage test.py

一切正常,但是当它从也作为 sage 包安装的 django 视图调用时,我得到了提到的错误

如果有人能帮助我,我会很高兴,因为我正在做一个重要的项目,我真的需要使用 sage。

我在 python 文件中设置了 env 变量,该文件是从 django 视图函数调用的。它已经解决了。您需要设置以下变量:

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