How to properly import sage?

user2391236 picture user2391236 · Jul 8, 2014 · Viewed 7k times · Source

Edit: I need to clarify, I want to import sage as a library to use in Python scripts OUTSIDE of the sage shell because I need it to run as a server on Django.

Do I have to compile sage from source?

I have been trying to use sage for my python scripts.

Code looks like this:

#!/usr/bin/env sage -python
from django.shortcuts import render
from django.http import HttpResponse
import sys
from django.http import HttpRequest
from django.template import RequestContext, loaders
from sage.all import *

def index(request):

    querystring = request.GET.get('querystring')

    return HttpResponse(querystring)
# Create your views here.

But I'm getting an error : no module named sage.all

I haven't had trouble running

#!/usr/bin/env sage -python
import sys
from sage.all import *
var('x')
print integrate(x,x)
print latex(integrate(sin(x),x))

From the command line with ./sage -python /path/to/script.py

So I don't understand why I can't import sage...

The directory "sage" IS in the python path, it's right next to the views.py file I'm trying to use it in, I've tried putting it in various different places, or appending it to the sys.path, to no avail. Any help is GREATLY appreciated, this is a very important project. I am trying to import Sage into a Django project.

Edit: I am NOT running the second one with ./sage -python, instead I am running it as views.py on my Django localhost server.

Answer

kcrisman picture kcrisman · Jul 8, 2014

To use from sage.all import * you need to be in a Sage shell, or at least have the right things defined. To make sure you have them, try adding

from os import environ
print environ

to your script. You should get things like PYTHONPATH and a bunch of Sage-specific things. So if you aren't running it with ./sage -python like your second example (I'm only saying this on the off chance you aren't) then I don't know. You would think the shebang line would already do this but maybe those don't take arguments, it seems that behavior on that is pretty variable by OS.

Edit: After some discussion elsewhere, I think that the problem is that you are trying to run a shell script using Python. This SO question is exactly what the doctor ordered.

To make the example explicit, I now have two files.

$ cat views
#!/usr/bin/env sage -python

from sage.all import *
print permutations(5)

$ cat views.py
import subprocess
subprocess.call(['./views'])

Now I can run this as an ordinary (no Sage shell) Python process.

$ python views.py 
./views:4: DeprecationWarning: Use the Permutations object instead.
See http://trac.sagemath.org/14772 for details.
  print permutations(5)
[[1, 2, 3, 4, 5], [1, 2, 3, 5, 4], [1, 2, 4, 3, 5], [1, 2, 4, 5, 3], [1, 2, 5, 3, 4], [1, 2, 5, 4, 3], [1, 3, 2, 4, 5], [1, 3, 2, 5, 4], [1, 3, 4, 2, 5], [1, 3, 4, 5, 2], [1, 3, 5, 2, 4], [1, 3, 5, 4, 2], [1, 4, 2, 3, 5], [1, 4, 2, 5, 3], [1, 4, 3, 2, 5], [1, 4, 3, 5, 2], [1, 4, 5, 2, 3], ... , [5, 4, 3, 2, 1]]

I am sure there is a more elegant way to do this, but for now I think this should be sufficient for you. Make sure that you don't give the controller access to just any old files, by the way - I am not a security expert.