5

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.

  • Hmm, if the second one works, so should the first (assuming you are invoking it also with `./sage -python`); have you tried moving the django imports below the Sage one? (I don't know why that would help, but might as well try.) – kcrisman Jul 08 '14 at 16:41
  • yes, i just tried that, it did not help –  Jul 08 '14 at 22:36

3 Answers3

2

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.

Community
  • 1
  • 1
kcrisman
  • 4,374
  • 20
  • 41
  • I am not running the one that doesn't work in the command line because I need it to work as a server. –  Jul 08 '14 at 17:05
  • is there no way that I can run the code outside of the sage shell? people have set up sage servers before so clearly there is a way.. –  Jul 08 '14 at 17:09
  • I'm treading carefully here, because I'm not an expert on this. But my understanding is that even in a Sage notebook server, the actual processes set up are forks or something of a Sage process. One way to do this might be to set up a "fake sage python" script that actually internally calls `sage -python`. But I feel like your problem is more that than anything else. – kcrisman Jul 09 '14 at 00:20
  • I am not using sage notebook, do I need to? And could you provide an example of how I'd have to edit my code? –  Jul 09 '14 at 01:58
  • No, you shouldn't have to use the notebook. – kcrisman Jul 09 '14 at 12:54
  • So the first one is a shell script... Or a python script? And where is it stored? –  Jul 09 '14 at 15:09
  • `views` is shell, `views.py` is Python. You can tell because the first one has a "shebang" while the second one is just Python. I guess you could store them anywhere you want, though as I have written them you'd need them together. – kcrisman Jul 09 '14 at 18:43
  • forgive me, id like to try your solution, but i dont know how to create a shell script... should i have views.sh? –  Jul 10 '14 at 15:56
  • also, putting "./" in my python file gives me an error –  Jul 10 '14 at 15:58
  • The filename is not crucial. The code I posted works in my case. Note that I had to make `views` executable by me (use `chmod` etc.). – kcrisman Jul 13 '14 at 00:37
  • I don't mean to sound harsh - it took me years to accumulate the very minimal shell knowledge I have - but I'm wondering whether a shell and Unix tutorial might be what the doctor ordered here? A lot of your questions aren't strictly speaking Sage questions, and are not ones we're ideally positioned to help answer. Alternately, you may want to try to do a G+ hangout or some other "live" help session ... anyway, I really am trying to be helpful here, I hope it is useful. – kcrisman Jul 13 '14 at 00:37
0

Have you tried from yor.app.sage.all import *, assuming sage is inside your/app/ directory.

Darwin
  • 1,809
  • 15
  • 17
-1

Sage is not a pure Python library. It needs and relies on hundreds of shared libraries to do its work. Some of which are compiled against the CPython API and depend on the Python version. That is why you can't just import sage.all from any Python interpreter that you might have laying around.

More importantly, stop what you are doing right now and think about the security implications. Evaluating arbitrary code in the web server process has ALWAYS ended up in tears. You think you can sanitize user input? No! You think nobody will expose this server to the internet? No! Its going to happen, and it will end badly.

vbraun
  • 1,851
  • 17
  • 14
  • Thank you for you input, I'd be interested to hear what you think would be needed to MAKE IT WORK. As far as your warning, the server will only be receiving mathematical sage code, I'm not worried about that. –  Jul 09 '14 at 15:07
  • "I'm not worried about that" = I already have a way to ensure that the only incoming queries that will be accepted will be coming through registered accounts and will be verified as being viable math queries before they even are sent. –  Jul 09 '14 at 15:53
  • "will be verified" really means: "my server will be hacked". You can't validate user input in a Turing-complete language. – vbraun Jul 09 '14 at 23:12
  • Ah. so I take it your recommendation is to completely give up on my project, I will be hacked? I fact no math servers should ever exist? They are literally impossible to make secure? What about wolframalpha? –  Jul 12 '14 at 20:58
  • It can be made secure as long as you never evaluate in the process space of the web server! Computations must run in isolation. – vbraun Jul 18 '14 at 00:55